FreeAgent sign-up

On moving a directory-based Wordpress installation to a subdomain

When I originally cobbled this blog together the URL I decided upon was http://www.lylo.co.uk/blog which seemed like a good (easy) idea at the time, but I recently decided that what I wanted the URL to be was http://blog.lylo.co.uk because, quite frankly, subdomains are cool.

Recently I moved web hosts from AISO to Slicehost, mainly to give myself hours and weeks of pain by building my own virtual Linux server, but also to host and manage client sites myself. I built Apache from source and Slicehost provide full DNS control so I can now create as many subdomains as I want and not pay a penny. If any web host out there is charging you, say, £5 a month per additional subdomain then you should really think about moving. I come cheap and highly recommended :-)

Adding the blog subdomain was as easy as adding a VirtualHost declaration in my Apache config:

<virtualhost>    
ServerAdmin admin@lylo.co.uk    
DocumentRoot "/the/path/to/htdocs/blog"    
ServerName blog.lylo.co.uk    
ErrorLog "/the/path/to/logs/error_log"    
CustomLog "/the/path/to/logs/access_log" common
</virtualhost>

My existing Wordpress installation lived in the htdocs/blog directory, so once the new subdomain was active, requests to it automatically displayed the blog. However, none of the links in the blog were using the subdomain because Wordpress was still configured to work with the old http://www.lylo.co.uk/blog URL. I changed this to http://blog.lylo.co.uk in the Wordpress Options page and, magically, all the links appeared using the new subdomain URL.

If only they worked.

The problem was that the .htaccess file in the htdocs/blog directory was configured for the old /blog set up:

<ifmodule>
RewriteEngine On
RewriteBase /blog
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</ifmodule>

Since the blog directory wasn’t publically visible, I removed it from the config:

<ifmodule>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>

Which made all my blog permalinks work through the new subdomain. Huzzah!

Except that now all the old links which my thousands upon thousands of avid readers (Andy) have carefully bookmarked and refer to almost daily, all seamlessly failed at once. The best way to deal with this situation is a permanent 301 redirect from the old URL to the new. This way I don’t hamper Lylo’s top-notch Google ranking for searches such as “lylo” and, er, “lylo uk”. Putting a 301 redirect in my htdocs/blog directory would work perfectly if the location I was redirecting to wasn’t the same folder (and thus causing a recursive redirect which Apache really doesn’t like), so I moved the Wordpress installation to a new directory:

[htdocs]$ cp -R blog wordpress

I then changed the Apache VirtualHost setting to point to the new directory before I removed everything from the old blog directory. Finally, to redirect all the old /blog requests, I added the 301 redirect by creating an .htaccess file in htdocs/blog containing the following statements:

RewriteEngine ON
RewriteRule ^(.*)$ http://blog.lylo.co.uk/$1 [R=301,L]

And, as Tony Blair once said, that is that. The end.

Posted by Olly on June 28, 2007 at 12:12 pm in apache, wordpress
| Permalink

One comment on “On moving a directory-based Wordpress installation to a subdomain”

  1. Andy Stewart wrote on June 29th, 2007 at 10:03 am:

    This sounds like stomping up a mountain where you think each peak ahead is the summit, only to get there and find another one further ahead. I bet you were gladder than Mr Blair to get to the end!

    Next time my Apache breaks down I’ll know who to call…no, not Tony.