Use symlinks to serve subdomain content

When you add a subdomain to your site, many hosting providers will automatically create a new physical folder on the server to match the subdomain name and to serve the subdomain content from. Which is all fine and good if your subdomain has a completely different content from main site/other subdomains.

But there’re situations when you need several subdomains to point to the same physical folder. Case in point: WordPress multisite install. There’s only a single installation of WordPress software, but depending on how you access it – it will serve different content. For example if you visit http://kitchen.galanter.net you will see a culinary blog, but if you open http://codecorner.galanter.net – a programming content will be served to you. This is achieved by WordPress by reading host header information (HTTP_HOST) and depending on header name (“kitchen” or “codecorner”) different content will be served. But it will be served from the same physical folder

Many hosting providers allow you to select root folder for the subdomains – in this case it’s easy: just add subdomain, point it to the root folder of the main domain and you’re done. But some, like my current host hard-code subdomain-subfolder relationship and it’s not allowed to change. In my case my root domain (which serves http://www.galanter.net) is physically located in /galanter.net/public folder. When I added subdomain codecorner, provider automatically created folder /galanter.net/codecorner/public which would have a completely different content, but I needed its content to be served from main /galanter.net/public folder, just pass “codecorner” HTTP host header to it. Enter symlinks.

Symbolic Links (symlinks) are shortcuts to real files and folders (pretty much like Windows shortcuts). So the idea here is substitute real public folder inside of codecorner with symlink to public folder inside of /galanter.net. One way to do it is via shell access to the site. So this is what I did:

  • Deleted real public folder from codecorner
  • Connected to my site via SSH and went inside of codecorner
    cd /galanter.net/codecorner
  • Created a symlink to the public folder of /galanter.net
    ln -s ../public public

And that’s it! After that as far as anything or anybody is concerned there’s a public folder in codecorner. It’s not real, in fact it’s a public of /galanter.net, so in effect same folder as main domain with single one-and-only WordPress install. And the result you see before your eyes 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *