【发布时间】:2017-01-12 00:37:09
【问题描述】:
问了数百万次的问题,但是在阅读了这里的许多答案之后,我仍然无法弄清楚这一点。我需要重定向所有请求,如下所示:
- http://domain.tld > https://domain.tld
- http://www.domain.tld > https://domain.tld
- https://www.domain.tld > https://domain.tld
- http://sub.domain.tld > https://sub.domain.tld(当.htaccess 放在子域文件夹中时)
到目前为止,我一直在使用 html5boilerplate 中的代码,将 www 解决为非 www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
</IfModule>
他们还有一个 http 到 https 重定向的代码,但是在添加这段代码后(在 www 重定向之上),页面正在加载并且在超时后它显示错误/重定向太多
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
我也尝试过this SO answer 之类的示例,但该网站仍然无法正常工作。目前唯一的解决方案是使用第一段代码并将 http 替换为 https 但这并不能解决最重要的重定向 (1)
当前完整的.htaccess 内容:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
#RewriteCond %{HTTPS} off [OR] #by uncommenting this, site stops working
RewriteCond %{HTTP_HOST} ^www
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
</IfModule>
【问题讨论】:
标签: .htaccess redirect mod-rewrite