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!

5 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]

Leave a Reply