Search code examples
apache.htaccessdrupaldrupal-domain-access

Redirecting all subdomains to a domain


I am having an issue with one of my sites where I need to redirect all subdomains to the main domain name. I have a Drupal site set up that shares content between 2 domains (using the Domain module). Here is basically what is going on.

In my .htaccess file I have these rules..

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

This redirects any subdomain to mydomain.com, which is great!

The problem is that I have another domain (myotherdomain.com) which uses the same Drupal site to share content with via the Domain module.

With that .htaccess rule in place when I go to myotherdomain.com it is redirecting to mydomain.com which I do not want to happen. Is there any way that I can stop that from happening?

To recap:

  1. anything.mydomain.com needs to redirect to mydomain.com
  2. any traffic to myotherdomain.com needs to stay at myotherdomain.com and not get redirected.

Solution

  • Just add another exclusion condition for your other domain:

    RewriteCond %{HTTP_HOST} !^myotherdomain\.com$ [NC]
    RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
    RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301] 
    

    This way, any request for "myotherdomain.com" won't get redirect, and any request for "mydomain.com" won't get redirected.