skip to Main Content

I’m trying to set a few rules in my .htaccess to work in a specific way:

  1. Specific page redirections:

    RewriteRule "^web/site/mais$" https://www.main.com/front/for-you/insurance/ [R=301,L]
    
  2. Home redirection

    RewriteRule ^$  https://www.forexample.com.br/for-you/ [R=301,L]
    RewriteRule ^/$  https://www.forexample.com.br/for-you/ [R=301,L]
    
  3. HTTP to HTTPS and add www to the rest (this is what doesn’t work), i tried many things:

    RewriteCond %{HTTP_HOST} !^www.
    RewriteCond %{HTTPS} !^on$ [OR]
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{HTTP_HOST} ^www.
    RewriteCond %{HTTPS} !^on$ [OR]
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

Other way I tried forgotting the www rule (but doesn’t work too):

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The only way I found to combine is writting the general https and www rule in the httpd.conf and the other rules into the .htaccess, but this way I get two or more 301 redirects, bad business for SEO.

2

Answers


  1. If you want to redirect some spacific pages to https, forexample, To redirect :

    to

    And

    to
    https://www.example.com/page2.html

    You may try the following :

     RewriteEngine on
     RewriteCond %{HTTP_HOST} !^www. [OR]
     RewriteCond %{HTTPS} off
     RewriteRule ^(page|page2).html$ https://www.example.com/$1.html [L,R]
    

    If you want to redirect the whole site from http to https, you can use the following :

     RewriteEngine on
    
    
     RewriteCond %{HTTP_HOST} !^www. [OR]
     RewriteCond %{HTTPS} off
     RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]
    
    Login or Signup to reply.
  2. Do you have the same problema with all the directories or only with some?

    Because if you have ProxyPass (as you said), maybe it send you the redirections directly to Tomcat, so maybe you need to specify the rules into the ssl.conf to read that rules before leave the ssl.conf. Try to write the last you wrote into the ssl.conf after the ProxyPass

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search