.htaccess强制在htm页面上使用HTTPS和非www

时间:2016-01-29 02:27:08

标签: .htaccess ssl

更新: 经过几个小时的搜索仍然没有解决方案,所以我不得不更改/更新我的问题:

我有几页参数manualy使用以下代码转换为.htm:

RewriteRule ^MyPage1.htm$ cart.php?rid=1 [L,NC]

我已经测试了不同的解决方案,而且没有完全适用的工作:

代码1:

RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

结果1:

http://example.com/randompage.php - > http://example.com/randompage.php [不工作] http://www.example.com/randompage.php - > https://example.com/randompage.php [工作] https://www.example.com/randompage.php - > https://example.com/randompage.php [工作]

http://example.com/MyPage1.htm - > http://example.com/MyPage1.htm [不工作] http://www.example.com/MyPage1.htm - > https://example.com/MyPage1.htm [工作] https://www.example.com/MyPage1.htm - > https://example.com/MyPage1.htm [工作]

代码2

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{http_host} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

代码3

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteCond %{http_host} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

结果2& 3 (结果相同)

http://example.com/randompage.php - > https://example.com/randompage.php [工作] http://www.example.com/randompage.php - > https://example.com/randompage.php [工作] https://www.example.com/randompage.php - > https://example.com/randompage.php [工作]

http://example.com/MyPage1.htm - > http://example.com/MyPage1.htm [不工作] http://www.example.com/MyPage1.htm - > https://example.com/MyPage1.htm [工作] https://www.example.com/MyPage1.htm - > https://example.com/MyPage1.htm [工作]

2 个答案:

答案 0 :(得分:2)

正如Mike Rockett在评论中提到的,请确保重定向规则在RewriteRule ^MyPage1.htm$ cart.php?rid=1 [L,NC]之前。您不希望[L]停止评估以后的规则。

如果没有办法,请尝试将LogLevel trace6放入.htaccess中,然后查看Apache错误日志以查看重写规则实际发生的情况,从而启用高级日志记录。

另外,在开发重定向时可能会让你陷入困境的另一件事是它们可以被缓存,所以即使你解决了问题,事情仍然会被打破。我并不是说在这种情况下可能就是这种情况,而只是需要注意的事情。我建议您使用[R]代替[R=301],直到您确认它正在努力避免此问题。如果您怀疑缓存,可以尝试打开另一个尚未使用的浏览器,例如IE / Firefox / Chrome。

答案 1 :(得分:0)

只需将OR运算符与单个协议和域特定规则一起使用:

RewriteEngine On

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301,NE]