.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条回答
smile是对你的礼貌
2楼-- · 2020-02-08 18:38

Use this if you don't want the address bar to show the subfolder. It will redirect yourdomain.com to yourdomain.com/subfolder but will look like you are still on yourdomain.com

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourprimarydomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$ 
RewriteRule ^(/)?$ subfolder/index.php [L]
查看更多
登录 后发表回答