简体   繁体   中英

Redirecting from http to https using htaccess except for certain subdirectories

I recently learned that Godaddy provides a code for their customers who purchased their SSL certificate to redirect http to https using htaccess file. The code is shown below:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

However, there are about 10 subdirectories that I do not wish to redirect. What would be the most efficient way to exclude those directories?

Also, is the code above compatible with Cloudflare's CDN? I learned that some loops might occur. Thanks

You should be able use this, just place the directories that you do not want to be HTTPs inside the brackets:

RewriteEngine On

#Force everything to HTTPs
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]

#Checks if HTTPs is on for foobar & foobar2, if it is, turn it off
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(foobar|foobar2)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

You can obviously add as many directories as you like to this, just separate them with | .

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