简体   繁体   中英

HTTPS redirect for directory doesn't work correctly

My goal is to force HTTPS on the admin directory of my site. However, using the usual way give an unusual result. Here is the content of .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 .

So it sort of creates the admin/ part of the URL twice.

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.

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.

Why is this happening and what's the solution?

It is due to /admin/ in your target URL. Change your rule to this:

RewriteEngine On 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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