.htaccess:http到https重定向不起作用

时间:2016-01-08 09:09:19

标签: .htaccess http mod-rewrite redirect https

我的.htaccess和https重定向存在问题。

首先,我已经查阅了这些链接以找到解决方案,但没有人可以帮助我。

链接列表:

当我使用http加载页面时,.htaccess会将我重定向到https,但是当我使用https加载页面时,我有一个无限循环。

这是我的.htaccess代码:

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{HTTP_HOST} ^(api|www|dev|stefano|sav)\.
RewriteCond %{REQUEST_URI} !^/(api|www|dev|stefano|sav)/
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^(.*)$ %1/$1 [L]

有人可以帮我创建重定向条件(http到https)吗?

2 个答案:

答案 0 :(得分:6)

您要离开Rewrite Flags您需要告诉它强制使用R标志重定向,并且可选提供状态代码,建议使用301,302等。

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

顺便说一句,您链接的每个示例都使用R flag显示答案,不确定为什么您没有使用确切的示例:)

在负载均衡器等设备后面的解决方案。

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

答案 1 :(得分:1)

我一直在和同样的问题打架几个小时。似乎我需要负载均衡器后面的系统解决方案。也许这是因为我在我的网站上使用Cloudflare。

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

将上述行添加到我的.htaccess文件后,我删除了Cloudflare上的缓存,并且网站按原样加载。