数百万次的问题,但在阅读了许多有关此处的答案后,我仍然无法弄清楚。我需要重定向所有请求,如下所示:
到目前为止,我使用的是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>
我也试着像例子这样SO答案,但仍然网站无法正常工作。目前唯一的解决方案是使用第一段代码并替换为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>
有时,根据某些服务器配置,似乎HTTPS
变量始终设置为off
。这将解释循环,并且我已经在SO上看到过类似的情况。
一个简单的解决方法是进行测试SERVER_PORT
(如果您使用默认的80和443),或者也可以进行测试HTTP:X-Forwarded-Proto
。
示例(1)测试是否不是 https
:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
实施例(2),以测试它是否不 https
:
RewriteCond %{SERVER_PORT} !403
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句