简体   繁体   中英

.htaccess; redirect all https:// to http:// expect of two domains

I have this code to redirect all https:// to http:// :

RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

But how can I exclude two domains www.example.com and www.test.com from this rule?

You can use a negative RewriteCond :

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(?:www\.)?(?:test|example)\.com$ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Make sure to clear your browser cache before testing this change.

You need to include two Rewrite Conditions:

RewriteCond %{HTTP_HOST} !^www\.example\.com%{REQUEST_URI} [NC,OR]
RewriteCond %{HTTP_HOST} !^www\.test\.com%{REQUEST_URI} [NC]

This will excluded both of those domains as well as any directories within the domain and keep them as HTTPs. So your code should now look like:

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.example\.com%{REQUEST_URI} [NC,OR]
RewriteCond %{HTTP_HOST} !^www\.test\.com%{REQUEST_URI} [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Make sure you clear your cache before testing this.

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