Maverick
Maverick

Reputation: 61

301 redirect for entire site except for subdomains

I have setup a sitewide 301 redirect in my .htaccess as follows

RewriteEngine On 
RewriteCond %{HTTPS} !=on 
RewriteRule ^.* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 

But I wanted to exclude subdomains (support, blog) from being included in this redirect. So I add the following RewriteConditions

RewriteEngine On 
RewriteCond %{HTTP_HOST} !=support.example.com
RewriteCond %{HTTP_HOST} !=blog.example.com
RewriteCond %{HTTPS} !=on 
RewriteRule ^.* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 

This works totally fine. However I wonder if there is a better way to specify the exclusion for all subdomains (not WWW) in just 1 RewriteCond statement (instead of a separate RewriteCond for each subdomain)

I have a few more subdomains and plan to add some more. Would appreciate the help.

Upvotes: 1

Views: 1755

Answers (1)

anubhava
anubhava

Reputation: 784888

You can use this rule:

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

Upvotes: 2

Related Questions