我正在尝试制定一个.htaccess规则,我可以在我的主域名&子域名,无需更改每个子域名的代码。
我尝试过以下子域尝试强制使用https和非www,但它无法正常工作。
<IfModule mod_rewrite.c>
# Force HTTPS & NON-WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.test\.website\.com$ [NC] # Detect if it has www?
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^test\.website\.com$ [NC]
RewriteRule ^ https://test\.website\.com%{REQUEST_URI} [R=301,L]
</IfModule>
目前,如果我要转到http://www.test.website.com/test/
,它会重定向到https://test%2Cwebsite.com/
,所以它有点有用但不完全。
我一直在尝试使用以下方式更加动态地执行此操作:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
但这根本不起作用。实现这一目标的最佳解决方案是什么?
它应该基本上强制执行以下所有操作:
http://website.com/ => https://website.com/
http://www.website.com/ => https://website.com/
https://www.website.com/ => https://website.com/
http://website.com/testing/ => https://website.com/testing/
http://www.website.com/testing/ => https://website.com/testing/
https://www.website.com/testing/ => https://website.com/testing/
http://test.website.com/ => https://test.website.com/
http://www.test.website.com/ => https://test.website.com/
https://www.test.website.com/ => https://test.website.com/
http://test.website.com/testing/ => https://test.website.com/testing/
http://www.test.website.com/testing/ => https://test.website.com/testing/
https://www.test.website.com/testing/ => https://test.website.com/testing/
答案 0 :(得分:4)
您可以使用此单一动态规则来实现所有http -> https
和www -> non-www
重定向:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
确保此规则位于顶部,并在测试此更改之前清除浏览器缓存。
答案 1 :(得分:1)
您的重写规则必须是(在第一个示例中):
RewriteRule ^/(.*) https://test.website.com/$1 [R=301,L]
删除反斜杠,因为重写目标不是正则表达式。
对于您的整体解决方案,我猜您需要两次单独的重写。一个用于执行https检查,另一个用于删除www。我想在一次重写中是不可能的,因为你需要检查两个条件,其中只有一个可能匹配。
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^/(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ https://%1/$1 [R=301,L]
答案 2 :(得分:1)
试试这个:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# First, force the HTTPS, whatever it is:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Then drop the www, if any:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule .* https://%1%{REQUEST_URI} [L,R=301]
</IfModule>
## Results:
# http://website.com/ => https://website.com/
# http://www.website.com/ => https://website.com/
# https://www.website.com/ => https://website.com/
# http://website.com/testing/ => https://website.com/testing/
# http://www.website.com/testing/ => https://website.com/testing/
# https://www.website.com/testing/ => https://website.com/testing/
# http://test.website.com/ => https://test.website.com/
# http://www.test.website.com/ => https://test.website.com/
# https://www.test.website.com/ => https://test.website.com/
# http://test.website.com/testing/ => https://test.website.com/testing/
# http://www.test.website.com/testing/ => https://test.website.com/testing/
# https://www.test.website.com/testing/ => https://test.website.com/testing/