Skip navigation

For full article see: Redirecting subdomains to directories in apache.

Note: It is likely you will need root access to carry out apache config alterations

If you want to be able to redirect blah.domain.com to domain.com/blah/ so that blah.domain.com is shown in the address bar here’s a quick and dirty how to:

First off if you want any subdomain to be able to be redirected first you need to set-up your DNS correctly. In my case this was as simple as going to the people that host my domain and adding an A record with the wildcard * that pointed to my server’s ip address. Bear in mind your DNS settings may take a while to work so be patient. Before you proceed you should check that anything.domain.com resolves to the ip address of your server. You can check this by running:

ping iloveapache.domain.com

This should respond with something like this:

[root@cheekymonkey conf]# ping iloveapache.domain.com
PING iloveapache.domain.com (62.193.xxx.xxx) 56(84) bytes of data.
64 bytes from my.serverhostame.com (62.193.xxx.xxx): icmp_seq=0 ttl=64 time=0.058 ms
64 bytes from my.serverhostame.com (62.193.xxx.xxx): icmp_seq=1 ttl=64 time=0.040 ms
64 bytes from my.serverhostame.com (62.193.xxx.xxx): icmp_seq=2 ttl=64 time=0.071 ms
64 bytes from my.serverhostame.com (62.193.xxx.xxx): icmp_seq=3 ttl=64 time=0.028 ms

The next step is to set-up the virtual host to respond to the wildcard subdomains. In the case of plesk you have to set-up a vhost.conf file but on any other apache installation you can just edit httpd.conf and look for the <VirtualHost> that corresponds to your site. If you are using Plesk you will need to ssh in to your server and create a vhost.conf file if it doesn’t already exist here where <domain.com> is your domain:

/var/www/vhosts/<domain.com>/conf/vhost.conf

Open that file with the command line editor of your choice and add the following directives:

ServerAlias *.domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
RewriteCond /var/www/vhosts/domain.com/httpdocs/%1 -d
RewriteRule ^(.*) /%1/$1 [L]

Substitute domain.com for your domain. Now here’s a quick walkthrough how these rules work. The first line ServerAlias *.domain.com tells apache to accept anysubdomain.domain.com.

Next the rewrite rules work as follows:

  1. The first line of mod_rewrite !^www.* only matches urls that don’t begin with www. You don’t want to redirect http://www.domain.com to http://www.domain.com/www/ do you? Note: you could add additional lines like these to prevent re-writing other subdomains like secure.domain.com if that should not be redirected to domain.com/secure/
  2. The second line ^([^\.]+)\.*$ captures into a backreference anything that doesn’t have a period e.g. the subdomain. The plus makes sure that there is at least one character that matches.
  3. The next condition checked is that the directory captured into the backreference does actually exist hence the -d.
  4. This last line ^(.*) /%1/$1 [L] takes the requested path and dumps it into a back reference and then rewrites it to point to the a directory (the subdomain backreference captured in the Rewrite conditions above) . In other words %1 is the subdomain backreference and $1 is the path backreference (that captured by .*). The important thing to rember is you can refer to backreferences in rewriteCond lines with % and backreferences in rewriteRules with $. Lastly the [L] (last) means doen’t further rewrite this URL. Note: You might want to leave this out if you are doing any subsequent rewrites, be sure to test what you have set-up thoroughly!!!

Now once you have made those changes if you are running plesk you will need to apply your vhost.conf changes with the following command:

/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=<domain.com>

And then whether are using Plesk or not you need to restart apache e.g:

service httpd restart
Advertisements

4 Comments

  1. Hello,

    This article is exactly what I have been looking for! Thank you very much for this! I set it up so it looks like this:

    ServerAlias *.mydomain.com
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.* [NC]
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.usecupotea\.com
    RewriteCond /var/www/vhosts/mydomain.com/subdomains/%1 -d
    RewriteRule ^(.*) /%1/$1 [L]

    Where the difference is the subdomains directory instead of the httpdocs. I am using plesk on debian and get this error:

    -bash: d: No such file or directory

    When I run the following command:

    /usr/local/psa/admin/sbin/websrvmng –reconfigure-vhost –vhost-name=<d

    I get -bash: d: No such file or directory. So to rebuild I used a command I found on a different forum:

    /usr/local/psa/admin/bin/websrvmng -av

    This let my changes pickup and when I try to access anydomain.mydomain.com it goes to my index file for mydomain.com which is fine. But when I go to test.mydomain.com, which is a valid directory in my subdomains folder. I get:

    Not Found

    The requested URL / was not found on this server.

    Any help is greatly appreciated!

    Thanks!

    Trevor

  2. Oops.

    I noticed in my last post that i posted the URL to my site. Can you either remove that post or change usecupotea\.com to mydomain\.com? I don’t want many people knowing about our domain name yet. I noticed its still waiting moderation so no one can see it now. So if you could please make one of those changes for me it would be most appreciated! Also if you could delete this post also because it contains our domain name 🙂

    Thanks so much!

    Trevor

  3. Hi there, thanks for your post! I am going to give it a try on my test server before I do it on my production site. I have a question though: how would you remove or undo these changes? (in other words how do I restore the old httpd.include file if I wanted to for the original vhost/domain)

    Thank you,

  4. This is a very handy script. One question I have for you is this.

    Is there a way to give a 301 if there is no folder that matches the request?

    eg: subdir.domain.com would point to /var/www/subdir

    what about if subdir1.domain.com with no /var/www/sudir1?

    Currently it would point to domain.com

    Is there a way to return a 301?

    Thanks
    Dave


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: