简体   繁体   English

目录的HTTPS重定向无法正常工作

[英]HTTPS redirect for directory doesn't work correctly

My goal is to force HTTPS on the admin directory of my site. 我的目标是强制在我网站的admin目录上使用HTTPS。 However, using the usual way give an unusual result. 但是,使用通常的方法会得到不同寻常的结果。 Here is the content of .htaccess : 这是.htaccess的内容:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} admin
RewriteRule ^(.*)$ https://www.domain.com/admin/$1 [R,L]

However, when I try to open http://www.domain.com/admin/home.php , instead of redirecting to https://www.domain.com/admin/home.php it sends me to https://www.domain.com/admin/admin/home.php . 但是,当我尝试打开http://www.domain.com/admin/home.php ,没有将其重定向到https://www.domain.com/admin/home.php而是将我发送到https://www.domain.com/admin/admin/home.php

So it sort of creates the admin/ part of the URL twice. 因此,它会两次创建URL的admin/部分。

I tried changing the last line of .htaccess to RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L] (which doesn't make sense to me, but tried it still) and it redirected to the same wrong URL, which confused me further. 我尝试将.htaccess的最后一行更改为RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L] (对我来说没有意义,但仍然尝试了),重定向到相同的错误URL,这进一步使我感到困惑。

I'm not sure if it should matter, but I've also tried replacing [R,L] with [L,R=301] as suggested elsewhere on SO, with no avail. 我不确定是否应该这样做,但是我也尝试按照SO上其他地方的建议用[L,R=301]替换[R,L] ,但无济于事。

Why is this happening and what's the solution? 为什么会发生这种情况,解决方案是什么?

It is due to /admin/ in your target URL. 这是由于目标网址中的/admin/ Change your rule to this: 将规则更改为此:

RewriteEngine On 

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^admin(/.*)?$ https://www.domain.com%{REQUEST_URI} [R,L,NC]

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

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