为什么我进入了无尽的重定向周期?

时间:2019-01-11 14:12:17

标签: php apache yii2

我安装了基本的Yii2应用程序,并希望强制所有连接使用HTTPS而不是HTTP。这是我的.htaccess:

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule (.+)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index\.html
RewriteRule ^(.*)index.html /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/main\.html
RewriteRule ^(.*)main.html /$1 [R=301,L]

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward the request to index.php
RewriteRule . index.php

然后在web / index.php中添加

$_SERVER['HTTPS']='on';

当我打开网站时,我看到一个错误“重定向太多”。我检查了chrome网络检查器,发现从https://example.org重定向到https://example.org,所以进入了循环

1 个答案:

答案 0 :(得分:0)

我使用以下代码将http重定向到.htaccess文件中的https

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example/$1 [R=301,L]

它将端口80上的所有请求重定向到https。