Make all subdomains point to root unless otherwise specified

5,482

If you are in a shared hosting with a single directory, you can use mod_rewrite in .htaccess to redirect all the subdomains to the root.

Adapted from: https://stackoverflow.com/questions/5966827/redirecting-multiple-domains-urls-to-one-canonical-domain.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !^blog\.example\.com [NC]
RewriteRule ^/?(.*) http://example.com/$1 [L,R=301,NE] 
Share:
5,482

Related videos on Youtube

brentonstrine
Author by

brentonstrine

I like doing things manually. But I'm finally picking up on how incredibly useful all these new tools are.

Updated on September 18, 2022

Comments

  • brentonstrine
    brentonstrine over 1 year

    I'd like all subdomains that aren't being used for anything else to go straight to my domain.

    ns1.mydomain.com.   A   198.123.44.55
    ns2.mydomain.com.   A   198.123.44.66
    *.mydomain.com. CNAME   mydomain.com
    webdisk.blog.mydomain.com.  A   198.123.44.55
    

    However, say I have subdomain blog.mydomain.com which I still want to behave as a normal subdomain. I would like the following to happen:

         mydomain.com -->      mydomain.com
     www.mydomain.com -->      mydomain.com
     abc.mydomain.com -->      mydomain.com
    blog.mydomain.com --> blog.mydomain.com
    

    Is this possible?

  • brentonstrine
    brentonstrine over 11 years
    Dang, I don't have access to the config file... I'm on a shared hosting plan.
  • kkugelmann
    kkugelmann over 11 years
    Hey, maybe you should contact your shared hosting provider if it's possible to configure such a behaviour. The Shared Hosting Provider I'm using for my private Sites offers such a configuration.
  • phynam
    phynam over 10 years
    I am not familiar with Apache, but that seems to solve his problem at the server level for a shared hosting plan. However, I believe there will still need to be a DNS record to tell browsers where to navigate to. This can be accomplished by creating a Wildcard DNS record. To do this, create an A record pointing to the server with * as the subdomain.