如何仅将主页重定向到https而没有www?
http://www.mywebsite.com
http://mywebsite.com
https://www.mywebsite.com
以上所有URL应该重定向一次:
https://mywebsite.com
子目录中的所有内容都应保持不变
https://mywebsite.com/website-in-progress
我正在尝试找到的各种代码,但是在所有情况下都没有找到任何有效的代码。
编辑:这似乎在视觉上效果很好,问题在于某些网址具有多个重定向
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
其结果是:
- https://mywebsite.com/ - OK
- https://www.mywebsite.com/ - not desirable, redirects first to http://mywebsite.com and then to https://mywebsite.com
- http://www.mywebsite.com/ - not desirable, redirects first to https://www.mywebsite.com and then to https://mywebsite.com
- http://mywebsite.com/ - OK, redirects once to https://mywebsite.com/
答案 0 :(得分:1)
您可以将此规则用作最高规则:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^/?$ https://%1% [R=301,L]