.htaccess redirect subfolder to HTTPS

前端 未结 7 1765
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 01:41

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 connec

相关标签:
7条回答
  • 2020-12-14 02:23

    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]
    
    0 讨论(0)
  • 2020-12-14 02:25

    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]
    
    0 讨论(0)
  • 2020-12-14 02:27

    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]
    
    0 讨论(0)
  • 2020-12-14 02:37

    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]
    
    0 讨论(0)
  • 2020-12-14 02:40

    Try This

    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
    
    0 讨论(0)
  • 2020-12-14 02:41

    Try

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
    
    0 讨论(0)
提交回复
热议问题