Apache htaccess重定向到https://www
我有以下htaccess代码:Apache htaccess重定向到https://www,apache,.htaccess,mod-rewrite,redirect,https,Apache,.htaccess,Mod Rewrite,Redirect,Https,我有以下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} [
<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>
重新启动发动机
重写秒!{HTTPS}关闭
重写规则^(.*)$https://www.%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
重写cond%{HTTP_HOST}^www\。
重写规则^(.*)$https://www.%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
我希望我的站点被重定向到https://www.
使用HTTPS,并强制执行www.
子域,
但是当我访问
http://www.
(没有HTTPS),它不会将我重定向到https://www
使用HTTPS。要首先强制使用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]
关于代理
在某些代理形式之后,即客户端通过HTTPS连接到代理、负载平衡器、乘客应用程序等,
%{HTTPS}
变量可能永远不会打开
,并导致重写循环。这是因为即使客户端和代理/负载平衡器正在使用HTTPS,您的应用程序实际上也在接收普通HTTP流量。在这些情况下,请检查X-Forwarded-Proto
头,而不是%{HTTPS}
变量 Michal的答案对我来说很有用,尽管有一点修改:
问题:
当您拥有一个单站点安全证书时,,一个试图在没有https://www(或证书覆盖的任何域)的情况下访问您的页面的浏览器将在收到重定向到安全正确的https页面之前显示一个丑陋的红色警告屏幕
解决方案
首先使用重定向到www(或证书覆盖的任何域),然后才使用https重定向。这将确保您的用户不会遇到任何错误,因为您的浏览器会看到不包含当前url的证书
#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]
有很多解决方案。这里有一个指向ApacheWiki的链接,它直接处理这个问题
如果您使用的是CloudFlare或类似的CDN,那么在这里提供的%{HTTPS}解决方案中会出现无限循环错误。如果您是CloudFlare用户,则需要使用以下选项:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
要将http://或https://重定向到,可以在所有版本的apache上使用以下规则:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
Apache2.4
RewriteEngine on
RewriteCond %{REQUEST_SCHEME} http [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
请注意,%{REQUEST_SCHEME}变量可供使用,因为apache2.4如果您使用的是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
#开始SSL重定向
重新启动发动机
RewriteCond%{HTTP:X-Forwarded-Proto}=HTTP
重写规则^https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
#结束SSL重定向
这将把您从重定向循环中解救出来,并将您的站点安全地重定向到SSL
另外,最好检查一下mod_rewrite.c 这是我为代理而不是代理用户找到的最好方法
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
糟糕的解决方案和原因!
永远不要使用下面的解决方案,因为当您使用他们的代码时:
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]
浏览器将转到:
http://example.com
然后重定向到:
https://example.com
https://www.example.com
然后重定向到:
https://example.com
https://www.example.com
这对服务器的请求太多
大多数被接受的答案都有这个问题
最佳解决方案和答案 此代码具有一个
[或]
条件,以防止在url上进行双重更改
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
在.htaccess文件中设置
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
我尝试了第一个答案,但它不起作用。。。 这项工作:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
重新启动发动机
重写基/
重写cond%{ENV:HTTPS}=在…上
重写规则^.*$https://%{SERVER_NAME}%{REQUEST_URI}[R,L]
#开始WordPress
重新启动发动机
重写基/
重写规则^index\.php$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则/index.php[L]
#结束WordPress
注意:确保已完成以下步骤
选项索引跟随符号链接多视图
允许超越所有
命令允许,拒绝
通融
要求所有授权
现在您的.htaccess将
工作,您的站点将重定向到http://to,类似于Amir Forsati的解决方案,但对于可变域名,我建议:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%2%{REQUEST_URI} [R=301,L]
我从这个网站使用了下面的代码,它工作得很好
重新编写引擎打开
重写cond%{SERVER_PORT}80
重写规则^(.*)$https://www.yourdomain.com/$1[R,L]
应该是RewriteCond%{HTTPS}=off
如果我这样做,它会重定向到HTTPS://w.w。亲爱的@bigben您在这里接受了一个错误的答案!您可以在my中找到错误的原因。还可以看到“在一个301重定向中强制执行https
和www
”,它不会将https://theurlwithoutwww.com重定向到https://www。如果您事先有一些“添加www”指令,这实际上是唯一有效的解决方案。其他解决方案为我提供了“到多个重定向”。更正这是我为代理而非代理用户找到的最好方法+1这是最好的方法,因为它解决了多个301重定向。这是有史以来最好的解决方案。回答得很好。这是唯一一个对我有用的。如果您支持Cloudflare(尽管我在Cloudflare上没有设置页面规则),则可以使用它。这是唯一适用于我所有案例的解决方案。但是,如果我想做反操作并删除所有“www.”“糟糕的解决方案,为什么!”——这些规则的顺序是错误的。反转这两个规则,它将是正确的-您将只得到一个重定向,而不是两个。是的,给定的代码将导致两个重定向-我不反对这一点-我所说的是,您只需要反转这些规则来解决此问题,不需要其他更改。(被“接受”的答案确实不正确——这似乎是wh
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%2%{REQUEST_URI} [R=301,L]