简体   繁体   中英

htaccess RewriteRule to https doesn't work combined with other rules

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.

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM