问题
I currently have mod_rewrite in place with code I found to redirect all requests to the same URL without www.
What I'm trying to do now is configuring Apache mod_rewrite to do the following, without any wasted actions/additional redirects.
http:// to https://
http://www. to https://
I'm not familiar with it, but in pseudo code, http(s)://(www.)domain.com
to https://domain.com
for solely the URLs at the top level, excluding sub domains.
回答1:
You just need one rule for both conditions:
put this code in your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,R=301,L]
回答2:
Yes, there is a way to combine these operations into a single rule:
RewriteCond %{HTTP_HOST} !=example.com [OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^/(.*) https://example.com/$1
回答3:
This one worked for me. I tried the one above but it created a redirect loop on older browsers.
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
来源:https://stackoverflow.com/questions/24593048/mod-rewrite-www-to-no-www-and-http-to-https-simultaneously