rockyraw rockyraw - 2 months ago 13
Apache Configuration Question

Exclude subdomain from Rewritecond

I didn't find a thread that had the same

RewriteCond
I currently have.

My root
.htaccess
is set up the following way, so it redirects and auto adds
www
to any URL that doesn't contain
www
:

Options -Indexes

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_USER_AGENT} ^(BadBot1|BadBot2) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule ^.* - [F,L]


However, I want users to be able to access a subdomain -
sub.mydomain.com
, so this subdomain needs to get excluded.

How to do that?

Answer

Add this line:

RewriteCond %{HTTP_HOST} !^sub\. [NC]

Before:

RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]