Easy subdomains with mod_rewrite

Because I like to play with a lot of projects and code, I create subdomains for felixdv.com almost every day. Instead of going through the hassle of creating a CNAME or A record for every subdomain, creating an Apache vhost file and enabling it and creating the necessary directory structure, I figured there’s an easier way to do so.

First thing to do is to create an *.example.com wildcard record for your domain. This way, you don’t need to create a DNS record every time you need a new subdomain. When this is done, check for the record for example using dig, and you should see something like this:

$ dig *.example.com
# .. skipping some output
;; ANSWER SECTION:
*.example.com.          3600    IN      A       192.0.32.10

Once that’s done and it’s pointing to your server, we need to create an Apache vhost which handles the requests with mod_rewrite, which is a rules-based url rewriting module for apache, and can easily handle different subdomains and actions based on the subdomain.

We’re assuming you’re replacing the domain name, IP address and paths with your own, and you have a directory structure like this:

/var/www/example.com/
/var/www/example.com/subdomains/
/var/www/example.com/subdomains/www/
/var/www/example.com/subdomains/<any subdomain>/

Apache vhost (stripped version):

<VirtualHost 192.0.32.10:80>
        ServerName example.com
        ServerAlias *.example.com
        DocumentRoot "/var/www/example.com"
 
        RewriteEngine on
        # Rewrite everything for example.com to the
        # subdomains/www/ subdirectory
        RewriteCond %{HTTP_HOST} ^example\.com
        RewriteRule ^(.*) /subdomains/www/$1 [L]
 
        # Rewrite all other example.com subdomains
        # to their own directory
        RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com
        RewriteCond /var/www/example.com/subdomains/%1 -d
        RewriteRule ^(.*) /subdomains/%1/$1 [L]
</VirtualHost>

This way, you can just create a directory like /var/www/example.com/subdomains/blog/ to make http://blog.example.com work. Easy like that!

6 Responses to “Easy subdomains with mod_rewrite”


  • Hi, thanks for article. I have a little problem with missing default apache icons.
    GET http:…/icons/back.gif 404 (Not Found)
    GET http:…/icons/blank.gif 404 (Not Found)
    GET http:…/icons/image2.gif 404 (Not Found)

    Could you please help, how solve this problem. Thank you!

  • @Belin: do you have any .htaccess files in the project that might interfere with the vhost setup?

  • Thanks for response. I have solved it, this way:
    RewriteEngine on
    # Rewrite everything for example.com to the
    # subdomains/www/ subdirectory
    RewriteCond %{HTTP_HOST} ^example\.com
    RewriteRule ^(.*) /subdomains/www/$1 [L]

    # Rewrite icons alias
    RewriteCond %{REQUEST_URI} !^/icons/

    Adding rewrite for icons.

  • What if i want to rewrite

    username.arena.co.ke to arena.co.ke/user.php?tag=username

  • Something like this should work:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.arena.co.ke
    RewriteCond %{HTTP_HOST} ^(.+).arena.co.ke
    RewriteRule ^([^/]*)$ http://arena.co.uk/user.php?tag=%1 [P,L]

  • Alex, I don’t think the Directory part is necessary in a .htaccess file beacuse the location of that file already implies the directory structure. What did the rest of it look like when you got the Forbidden error? I’m assuming that it was trying to resolve the path to your file system root and not your web root which is why you got the error. I’m not enough of an expert on Apache to really know how well your method holds up regarding efficiency but it would be nice to figure out a good method for those using shared hosting out there.p.s. I’m just running WordPress so I’m sure there’s some information out there regarding syntax in comments. I’ll have to take a look sometime since I’ve only ever posted code in the post itself and not in the comments.

Leave a Reply