htaccess redirect HTTPS to secure.domain.com, non

2020-08-01 06:30发布

问题:

Not sure how to best write the following rules:

  1. All secure (HTTPS) traffic should redirect to https://secure.domain.com
  2. Non-secure requests to secure.domain.com should redirect to https://secure.domain.com
  3. All other non-secure requests should go to http://www.domain.com
  4. Lastly, all non-www requests should redirect to www, except the "secure" subdomain.

回答1:

Try adding the following to your htaccess file in the root folder of your domain.

RewriteEngine on
RewriteBase /


#Non-secure requests to secure.domain.com should redirect to https://secure.domain.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^secure\.domain\.com$ [NC]  
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

#All secure (HTTPS) traffic should redirect to https://secure.domain.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^secure\.domain\.com$ [NC]  
RewriteRule .* https://secure.domain.com%{REQUEST_URI} [L,R=301] 


#All other non-secure requests should go to http://www.domain.com
#Lastly, all non-www requests should redirect to www, except the "secure" subdomain.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]  
RewriteRule .* http://www.domain.com%{REQUEST_URI} [L,R=301]