.htaccess-“重定向过多"尝试强制使用https时 [英] .htaccess - "Too many redirects" when trying to force https
问题描述
我试图强制我的根域的子文件夹(/bbb/)始终显示为https.另外,我的.htaccess
文件负责页面的扩展名.
I am trying to force a subfolder (/bbb/) of my root domain to show always as https. Also my .htaccess
file take care of the extensions of the pages.
我已将.htaccess
文件放在我的/bbb/
文件夹中,但是当我尝试强制连接到https时,出现重定向过多"的情况,一切正常.
I have put the .htaccess
file inside my /bbb/
folder but I get "Too many redirects" when I try to force to connect to https, without it everything works fine.
我的代码有什么问题?
Options +FollowSymLinks -MultiViews
Options +Indexes
AcceptPathInfo Off
RewriteEngine on
RewriteBase /
ErrorDocument 404 https://example.co.uk/404page/404.html
#Force from http to https
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^bbb.example.co.uk/$
RewriteRule ^(.*)$ https://bbb.example.co.uk/$1 [R=301]
#take off index.html
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
推荐答案
此规则存在问题:
#Force from http to https
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^bbb.example.co.uk/$
RewriteRule ^(.*)$ https://bbb.example.co.uk/$1 [R=301]
将此规则更改为:
#Force from http to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =bbb.example.co.uk
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301]
由于在开始时使用了!
,因此您的情况已经逆转,并且在末尾有一个额外的斜杠,该斜杠永远不会匹配,因此使您的情况总是返回true.
You have condition reversed due to use of !
at start and have an extra slash at end which will never be matched hence making your condition always return true.
在进行测试之前,请确保清除浏览器缓存.
Make sure to clear your browser cache before testing this.
这篇关于.htaccess-“重定向过多"尝试强制使用https时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!