简体   繁体   English

htaccess将http重定向到https创建重定向循环

[英]htaccess redirecting http to https creates redirect loop

I am trying to redirect my website from http to https using .htaccess file. 我正在尝试使用.htaccess文件将我的网站从http重定向到https。 The error I am getting is: website.com redirected you too many times. 我收到的错误是:website.com重定向您太​​多次。

Here is my htaccess file: 这是我的htaccess文件:

RewriteEngine On
RewriteBase /

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

The above works fine and does what it needs to do. 上面的工作正常,并且完成了它需要做的事情。 The problem comes from the following: 问题来自以下方面:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.com/$1 [R,L]


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

I have put "website.com" instead of my own domain in the example above. 在上面的示例中,我放置了“ website.com”而不是我自己的域。

I am using this code to redirect to www and https, and it is working. 我正在使用此代码重定向到www和https,并且可以正常工作。 it should be placed first in the .htaccess file. 它应该首先放在.htaccess文件中。 Errors with redirections can take some time to correct, redirections is cached for some time. 重定向错误可能需要花费一些时间才能纠正,重定向会缓存一段时间。

# BEGIN Custom redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# END Custom redirect

You might want something like this: 您可能想要这样的东西:

# BEGIN Custom redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# END Custom redirect

I used this : 我用这个:

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

instead of: 代替:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.com/$1 [R,L]

this fixed my problem 这解决了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM