使用.htaccess失败将httpredirect到https

我有一个使用HostGator上托pipe的CloudFlare灵活SLL的网站。

我想将所有 HTTP请求redirect到相应的HTTPS URL。 没有例外。 我打算把规则放在最上面,所以在处理完所有后面的重写规则的时候不应该testing。

我目前的代码是这样的:

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] 

但是这会导致无尽的redirect到HTTPS版本。 这里是FireFox Live HTTP Headers:

https://example.net/

GET / HTTP / 1.1
主持人: example.net
用户代理: Mozilla / 5.0(Windows NT 10.0; WOW64; rv:51.0)Gecko / 20100101 Firefox / 51.0
接受: text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8
Accept-Language: da,en-US; q = 0.7,en; q = 0.3
Accept-Encoding: gzip,deflate,br
DNT: 1
连接:保持活力
升级 – 不安全 – 请求: 1

HTTP / 2.0 301永久移动
date: 2017年2月15日(星期三)15:20:35 GMT
Content-Type: text / html; 字符集= ISO-8859-1
Set-Cookie: __cfduid = d07edac1644bccce1642d2c845767f9951487172035; 到期=星期四,15-Feb-18 15:20:35 GMT; path= /; 域= .example.net; 仅Http
位置: https //example.net/
服务器: cloudflare-nginx
cf-ray: 3319bea4dd2f3cfb-CPH
X-Firefox-Spdy: h2


http://ocsp.digicert.com/

POST / HTTP / 1.1
主持人: ocsp.digicert.com
用户代理: Mozilla / 5.0(Windows NT 10.0; WOW64; rv:51.0)Gecko / 20100101 Firefox / 51.0
接受: text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8
Accept-Language: da,en-US; q = 0.7,en; q = 0.3
Accept-Encoding: gzip,deflate
内容长度: 83
Content-Type: application / ocsp-request
DNT: 1
连接:保持活动0Q0O0M0K0I0 +

HTTP / 1.1 200 OK
接受范围:字节
Cache-Control: public,max-age = 172800
Content-Type: application / ocsp-response
date: 2017年2月15日(星期三)15:20:35 GMT
Etag: “58a44f61-1d7”
到期: 2017年2月22日(星期三)03:20:35 GMT
Last-Modified: Wed,15 Feb 2017 12:53:53 GMT
服务器: ECS(arn / 459D)
X-Cache: HIT
内容长度: 471


https://example.net/

GET / HTTP / 1.1
主持人: example.net
用户代理: Mozilla / 5.0(Windows NT 10.0; WOW64; rv:51.0)Gecko / 20100101 Firefox / 51.0
接受: text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8
Accept-Language: da,en-US; q = 0.7,en; q = 0.3
Accept-Encoding: gzip,deflate,br
Cookie: __cfduid = d07edac1644bccce1642d2c845767f9951487172035
DNT: 1
连接:保持活力
升级 – 不安全 – 请求: 1

HTTP / 2.0 301永久移动
date: 2017年2月15日(星期三)15:20:35 GMT
Content-Type: text / html; 字符集= ISO-8859-1
位置: https //example.net/
服务器: cloudflare-nginx
cf-ray: 3319bea7ddfb3cfb-CPH
X-Firefox-Spdy: h2


https://example.net/

GET / HTTP / 1.1
主持人: example.net
用户代理: Mozilla / 5.0(Windows NT 10.0; WOW64; rv:51.0)Gecko / 20100101 Firefox / 51.0
接受: text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8
Accept-Language: da,en-US; q = 0.7,en; q = 0.3
Accept-Encoding: gzip,deflate,br
Cookie: __cfduid = d07edac1644bccce1642d2c845767f9951487172035
DNT: 1
连接:保持活力
升级 – 不安全 – 请求: 1

HTTP / 2.0 301永久移动
date: 2017年2月15日星期三15:20:36 GMT
Content-Type: text / html; 字符集= ISO-8859-1
位置: https //example.net/
服务器: cloudflare-nginx
cf-ray: 3319beaaae7e3cfb-CPH
X-Firefox-Spdy: h2

我也看到过其他类似的问题,但是大多数build议的解决scheme都是我目前使用的一种变体,而且我已经尝试过了(但是请随意推荐任何适合您的工具,我会尝试的)。

Cloudflare灵活的SSL :您的访问者和CloudFlare之间的安全连接,但CloudFlare和您的Web服务器之间没有安全的连接 。 您的Web服务器上不需要SSL证书,但您的访问者仍然将该网站视为启用了HTTPS。 资源

因为您从服务器redirect到HTTPS,而不是使用Cloudflare页面规则,所以即使客户端的HTTPS请求仍会始终触发redirect规则。

 1. Client ---> HTTP ----> Cloudflare CDN ----> HTTP ----> Your server | <------- Response: Redirect to HTTPS <- 2. Client ---> HTTPS ----> Cloudflare CDN ----> HTTP ----> Your server | <------- Response: Redirect to HTTPS <- 3. Client ---> HTTPS ----> Cloudflare CDN ----> HTTP ----> Your server | <------- Response: Redirect to HTTPS <- 

Cloudflare不会将HTTPS与您的networking服务器通话,并创build一个无限redirect循环。

要解决这个问题,您需要从.htaccess文件中删除redirect,并设置一个Cloudflare页面规则。

 RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 

部分取自: https : //stackoverflow.com/a/26623196/2774776