Redirecting to a subdomain

It’s a familiar scenario. The client is looking to establish a web presence as quickly as possible, usually in the form of a blog, usually WordPress, which is usually installed in its own directory which is usually named “blog”.

While the main site is coming together we want people to see the blog when they go to the main URL, instead of the obligatory landing page.

We could do this via Javascript (added to the HEAD):

<script type="text/javascript">
<!--
window.location = "blog/"
//-->
</script>

Alternately, we could do it through a meta refresh (also added to the HEAD):

<meta http-equiv="refresh" content="0;URL=blog/">

Or, as the always on-it David Walsh suggests, if we’re going to do it either of these ways we should do it through a combination of both:

<script>
    window.location.href = "blog/";
</script>
<noscript>
    <meta http-equiv="refresh" content="0;URL=blog/" />
</noscript>

But it’s simpler and cleaner (and not dependent on the user having Javascript enabled in their browser) to use our pal, mod_rewrite. Create an .htaccess file and copy this into it (replacing “the-root-domain.com” with your domain):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?the-root-domain.com$
RewriteRule ^(/)?$ blog [L]

Save it to the hosting root and you’re in business. Just remember to adjust (or delete) this file when you start moving files for the main site to the root directory.