使用htaccess一起强制SSL和WWW [英] Force SSL and WWW together using htaccess
问题描述
我正在尝试通过https运行我的整个站点并强制使用www。
I'm trying to run my entire site through https and force www.
我已经看到了许多提供强制使用www或https的解决方案,并且甚至只有几个结合在一起,但我似乎什么也没用。我通常会发现自己处于重定向循环中。
I've seen a number of solutions that provide forcing either www or https, and even a few combined, but I can't seem to get any to work. I usually find myself in a redirect loop.
我最接近的是以下内容,但还不够接近:
The closest I've got is the following, but it's not close enough yet:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
我需要 https://www.example.com/
http ://example.com成功
https://example.com成功
http://www.example.com失败
https://www.example.com成功
,尽管没有实际的重定向。
http://example.com SUCCESS
https://example.com SUCCESS
http://www.example.com FAIL
https://www.example.com SUCCESS
, although there's no actual redirection.
谢谢
更新
以下代码成功执行了我要求的重定向:
Update
The following code successfully performs the redirection I require:
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
推荐答案
通过其他提供的答案组合来强制执行https和www。
Forcing https and www is done by a combination of other provided answers.
这似乎可行:
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
这篇关于使用htaccess一起强制SSL和WWW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!