Search code examples
.htaccesshttp-redirect

Avoid redirect htaccess redirect for subdomain


We currently use a redirect on our main domain (www.example.com) to automatically go to the main directory (www.example.com/main) when not one of the few allowed other directories using the below code;

RewriteCond %{REQUEST_URI} !/(logos|press|main)/ [NC]
RewriteRule ^(.*)$ /main/$1 [R=301,L]

This works great but we are now seeing an issue though when trying to use a subdomain (dev.example.com) will automatically reroute to www.example.com/main.

How would I maintain the normal redirect we do on our main page but allow for the dev.example.com subdomain to also be accessed?


Solution

  • Well, I assume that both hosts ("main domain" and "subdomain") are served from the same http server? If so you obviously need a condition that takes care that your redirection rule only gets applied if a specific host is requested ("www.example.com"):

    RewriteEngine on
    RewriteCond {HTTP_HOST} ^(www\.)?example\.com$
    RewriteCond %{REQUEST_URI} !^/(logos|press|main)/ [NC]
    RewriteRule ^ /main{REQUEST_URI} [R=301,L]