我一直在为一个愚蠢的问题而挣扎,我相信有人已经问过这个问题,但我无法在这里或其他地方找到正确的答复。所以我来了。我已经将我的.htaccess文件设置为将所有http请求重定向到https。重写也是启用的。但是,当我用http指定一个段时,站点重定向到https://example.com/index.php似乎一切正常。例如:
example.com -> redirects correctly to https://example.com
http://example.com -> redirects correctly
example.com/subdirectory -> redirects to https://example.com/index.php
http://example.com/subdirectory -> redirects to https://example.com/index.php
https://example.com/subdirectory -> redirects correctly
如果我在加载https://exmaple.com之后使用导航链接,那么一切都很好。
我的重写和重定向规则如下:
RewriteEngine On
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} (.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
我做错了什么?感谢您的时间,并感谢您的帮助。抱歉,如果这是一个重复,并将赞赏推动一个正确的方向。谢谢。
发布于 2019-09-14 16:17:16
让我们仔细研究一下:
[L]
我不太明白发生了什么,为什么失败了,但把最后一条规则作为第一条规则应该能解决它。
您还可以考虑切换REQUEST_URI,因为在路径之后会丢失任何查询字符串。相反,将最后一行改为:
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
这确保:
http://example.com
转到https://example.com
.
重复0次或多次(*
)后的任何字符(D20
)都放在第1组中。发布于 2019-09-14 19:00:59
由于我不明白的原因,将http移到https重定向对我很有帮助。所以我的档案现在看起来像这样。
RewriteEngine On
#Redirect from http to https
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#as @LTPCGO suggested, I think I will change the above line to the one below. but i've included to show that it works for now.
#RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
但是,我注意到,如果我在url中指定了index.php (例如,https://example.com/index.php/subdirectory ),则不会删除index.php。因此,我遵循@Frédéric的回答(Symfony 2's的做法),在这里找到了https://stackoverflow.com/questions/9608366/remove-index-php-from-url-with-htaccess。所以我现在的规矩是这样的,
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/index.php [L]
谢谢你@LTPCGO的回复,非常感谢。
https://serverfault.com/questions/984238
复制相似问题