Home : Internet : Server : Apache : Mod Rewrite : Catch-all Subdomains

Wildcard Catch-all Subdomains

Ever seen those sites with using anything.example.com as URLs? If you want this on your site, read on.

This actually not entirely the same as manually creating the subdomain in your control panel. We can emulate pretty much the same thing by creating the appropriate directory, or we can just pass the subdomain to a script.

We need to do three things in order to set this up - only the final stage involves mod_rewrite. On shared hosting, you will need to contact your host about stages one and two.

Point *.domain.com to your server

We can do this using a wildcard DNS. In your control panel, you should find a section on DNS management. This will allow you to add the wildcard record. Simply add an A record with the name:

*.domain.com.

And use the same IP as the existing records. You should now be able to access your server by going to "ANYTHING.domain.com". If you still get a Server Not Found error, you may need to wait for the DNS to update. Either way, you can move onto the next stage.

Tell your server to treat *.domain.com as domain.com

For Apache, we want to add a wildcard ServerAlias. Open up your httpd.conf, or more likely a custom include for each domain that contains a VirtualHosts section. If you see a line similar to:

ServerAlias www.domain.com domain.com

You want to add in there another ServerAlias for the wildcard:

ServerAlias www.domain.com domain.com *.domain.com

Note: If you wish to use "proper" subdomains as well, ensure the wildcard comes after the VirtualHost section for your subdomain.

Create the Rewrite

This code should be placed in the htaccess file in the root of your domain, i.e. domain.com/.htaccess

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   RewriteBase /
   RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
   RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
   RewriteRule (.*) %2/$1 [L]
<
/IfModule>

The IfModule wrapper ensures we only execute this code if the mod_rewrite module is enabled. See portability notes for more information.

Inside the IfModule, the first four lines are discussed and explained in Start Rewriting (part one). All we are doing is ensuring we are ready to rewrite.

We use two RewriteConds to first ensure we are not on the main domain and second capture the subdomain into a backreference. We are including an optional www. and this, is present will be in the %1 backreference, otherwise %1 will be empty. Our second set of parenthesis will match the subdomain, and so we capture that value into the backreference %2.

We then do the RewriteRule - how you do this is up to you. I have chosen here to turn a request for http://pie.domain.com/page.html into http://www.domain.com/pie/page.html.

If you wish to something different, you should be able to change the RewriteRule accordingly. If not, read our tutorial.

Subscribe to us on YouTube