【发布时间】:2017-01-09 15:32:20
【问题描述】:
我正在尝试制定一个 .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/
【问题讨论】:
-
"它重定向到
https://test%2Cwebsite.com/" - 那里发生了一些奇怪的事情,它与您在上面发布的指令无关。%2C是编码逗号?!这是从哪里来的? -
@w3dk 我也看不懂 %2C,网址中没有逗号,但它正在放一个。
标签: regex apache .htaccess mod-rewrite