big*_*ben 301 apache .htaccess mod-rewrite https redirect
我有以下htaccess代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond !{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Run Code Online (Sandbox Code Playgroud)
我希望我的网站可以重定向到https://www.
HTTPS,并强制执行www.
子域,但是当我访问http://www.
(没有HTTPS)时,它不会将我重定向到https://www
HTTPS.
Mic*_*ski 613
要首先强制使用HTTPS,您必须检查正确的环境变量%{HTTPS} off
,但是上面的规则会预先设置www.
由于您要执行第二条规则www.
,请不要在第一条规则中使用它.
RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)
当某些形式的代理背后,客户端通过HTTPS连接到代理,负载均衡器,乘客应用程序等时,该%{HTTPS}
变量可能永远不会on
导致重写循环.这是因为即使客户端和代理/负载均衡器使用HTTPS,您的应用程序实际上也会接收纯HTTP流量.在这些情况下,请检查X-Forwarded-Proto
标头而不是%{HTTPS}
变量.这个答案显示了适当的过程
Lar*_*zan 146
Michals的回答对我有用,虽然有一个小修改:
问题:
当您拥有单个站点安全证书时,尝试访问您的页面的浏览器不使用https:// www.(或者您的证书所涵盖的任何域)将显示丑陋的红色警告屏幕,甚至在它接收重定向到安全且正确的https页面之前.
解
首先使用重定向到www(或证书涵盖的任何域),然后才进行https重定向.这将确保您的用户不会遇到任何错误,因为您的浏览器会看到不包含当前网址的证书.
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)
And*_*rew 95
如果您使用的是CloudFlare或类似的CDN,则会在此处提供%{HTTPS}解决方案时出现无限循环错误.如果您是CloudFlare用户,则需要使用以下内容:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)
Ami*_*ati 55
不要使用上面的解决方案,因为当您使用的代码类似于:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)
浏览器转到:
http://example.com
Run Code Online (Sandbox Code Playgroud)
然后重定向到:
https://example.com
Run Code Online (Sandbox Code Playgroud)
然后重定向到:
https://www.example.com
Run Code Online (Sandbox Code Playgroud)
这对服务器的请求太多了
此代码有一个[OR]
条件,以防止URL上的双重更改!
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)
大多数答案甚至接受了一个,不要使用这个技巧.
lli*_*oor 33
这是我找到Proxy而不是代理用户的最佳方式
RewriteEngine On
### START WWW & HTTPS
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### END WWW & HTTPS
Run Code Online (Sandbox Code Playgroud)
小智 27
那里有很多解决方案.这是apache wiki的链接,它直接处理这个问题.
http://wiki.apache.org/httpd/RewriteHTTPToHTTPS
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context
Run Code Online (Sandbox Code Playgroud)
sta*_*een 11
要将http://或https://重定向到https:// www,您可以在所有版本的apache上使用以下规则:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
Run Code Online (Sandbox Code Playgroud)
Apache 2.4
RewriteEngine on
RewriteCond %{REQUEST_SCHEME} http [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
Run Code Online (Sandbox Code Playgroud)
请注意,自apache 2.4起,%{REQUEST_SCHEME}变量可供使用.
如果您使用的是CloudFlare,请确保使用类似的内容.
# BEGIN SSL Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# END SSL Redirect
Run Code Online (Sandbox Code Playgroud)
这将使您免于重定向循环,并将您的站点安全地重定向到SSL.
PS如果检查mod_rewrite.c是个好主意!
归档时间: |
|
查看次数: |
562100 次 |
最近记录: |