Moving WordPress from subdirectory to subdomain

In case you haven’t noticed, I’ve moved my blog yet again. This time, it is actually just an address change. The new address is blog.unauthorised.org.nz.

I expected it to be a straight-forward switch but it turned out to be a bit trickier than I anticipated.

The first thing I had to do was to update the WordPress address and Blog address in the Options tab. If you haven’t made this change, you’ll notice it right away as you will be redirected to an incorrect address. I’m not exactly sure what WordPress is doing under the hood but it kept redirecting me to the home page. I had a bit of trouble figuring this out at first as I thought it was a problem with my subdomain setup.

Once the WordPress addresses are updated, everything works nicely for blog.unauthorised.org.nz. But what about all the existing links pointing to www.unauthorised.org.nz/blog? It breaks of course. Although the files are actually still sitting under the blog folder, WordPress does some hocus-pocus and kept redirecting the address to blog.unauthorised.org.nz/blog, which of course does not exists.

At first, I was going to give up and wait for Google to recrawl my blog with the new address. Since I’ve completely changed the address, I might as well configure Permalinks to make the addresses look good too. PATHINFO Permalinks are great but that additional index.php in the urls bugs me. So, Pretty Permalinks it is. In the process, I learned about mod_rewrite.

An idea came to me, what if I can redirect all the old addresses of the form www.unauthorised.org.nz/blog to blog.unauthorised.org.nz? After a few unsuccessful attempts and Mozilla complaining about my handiwork not working correctly (as it ended up in an infinite loop of redirections), I finally came up with the solution.

## Redirect Unauthorised Blog addresses
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www.unauthorised.org.nz$ [OR]
RewriteCond %{HTTP_HOST} ^unauthorised.org.nz$
RewriteRule ^blog/(.*)$ http://blog.unauthorised.org.nz/$1

After adding those lines to the .htaccess under www.unauthorised.org.nz, all my old links started working again.

Leave a Comment

Name (required)

Mail (will not be published) (required)

Website

Comment