.htaccess redirect subfolder to HTTPS

2020-02-08 17:51发布

I'm trying to write a RewriteRule for my .htaccess file. Specifically, whenever a user accesses a specific subdirectory, I would like it to Rewrite to force an HTTPS connection.

For example, whenever someone accesses: http://www.mydomain.com/subdirectory (and any other sub-directories of that "subdirectory").

I'd like it to rewrite to https://www.mydomain.com/subdirectory

I've tried the following, but it appears to create a loop:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]

Also, this .htaccess file is placed in the root of my domain.

Any ideas on how to modify my RewriteRule?

Many Thanks!

7条回答
神经病院院长
2楼-- · 2020-02-08 18:15

Try This

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
查看更多
男人必须洒脱
3楼-- · 2020-02-08 18:20

I would put this into the domain's root directory:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/$1 [R=301,L]
查看更多
ゆ 、 Hurt°
4楼-- · 2020-02-08 18:22

Try

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
查看更多
霸刀☆藐视天下
5楼-- · 2020-02-08 18:26

This work for me, this allow you to redirect to https a specific folder, just add an htaccess file inside of the folder with the following content:

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
查看更多
贼婆χ
6楼-- · 2020-02-08 18:33

Add the lines given below to .htaccess file of that subdirectory:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/subdirectory/$1 [L,R=301]
查看更多
虎瘦雄心在
7楼-- · 2020-02-08 18:38

Ahh, its a combo of the two. MonoMano - you've omitted the subdirectory in the first part of the RewriteRule, therefore directing ALL traffic to the HTTPS subdomain address. I found that checking for 'off' was more reliable than checking for !=on, dont ask me why!

You'd want to add the subdirectory back in as per Floern's response, see below:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
查看更多
登录 后发表回答