我看过很多关于重写的文章,但我有一个问题。
example.com => https://example.com(确定)
www.example.com => https://example.com(确定)
https://example.com => https://example.com (确定)
https://www.example.com => https://example.com(失败)
他试了几个论坛解决方案,但我有同样的问题。现在我的 htaccess 是:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
</IfModule>
最佳答案
您可以使用:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
</IfModule>
在您的代码中,第二个测试没有使用 http 执行,%1 不正确。
关于.htaccess - 使用 SSL 将 www 重定向到非 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40822949/