Redirecting WordPress to a subdomain

After quite a few hours of messing with htaccess, I’ve finally figured out how to properly redirect my blog from http://nhnt11.com/blog to http://blog.nhnt11.com.

First of all, here were my redirecting goals:

  1. Avoid www:
    http://www.nhnt11.com/foo/bar -> http://nhnt11.com/foo/bar
  2. Remove trailing slashes from filenames:
    http://nhnt11.com/foo.bar/ -> http://nhnt11.com/foo.bar
  3. Redirect my blog:
    http://nhnt11.com/blog/foo/bar -> http://blog.nhnt11.com/foo/bar

You would think the htaccess rules would be simple. The obvious rules for each case respectively are:

  1. RewriteCond %{HTTP_HOST} ^www\.nhnt11\.com$
    RewriteRule ^(.*)$ http://nhnt11.com/$1 [L,QSA]
  2. # I don't want to mess with trailing slashes and stuff for the blog, so match only nhnt11.com.
    RewriteCond %{HTTP_HOST} ^nhnt11\.com$
    RewriteCond %{REQUEST_FILENAME} -f
    # Regex matches foo.bar/
    RewriteRule ^(.*\.[^/]+)/$ http://nhnt11.com/$1 [R=301,L]
  3. RewriteCond %{HTTP_HOST} ^nhnt11\.com$
    RewriteRule ^blog/(.*)$ http://blog.nhnt11.com/$1 [L,QSA,R=301]
    
    # nhnt11.com/blog -> blog.nhnt11.com (not covered in first rule)
    RewriteCond %{HTTP_HOST} ^nhnt11\.com$
    RewriteRule ^blog$ http://blog.nhnt11.com [L,QSA,R=301]

This should have done it. But no – for some reason, http://nhnt11.com/blog/foo/bar -> http://blog.nhnt11.com/foo/bar was refusing to work. It just gave me WordPress’s “Page not found” page.

After hours of tinkering (among other things, I could never be sure of what was working or whether my changes were being used – turns out browsers cache redirects so I had to keep clearing caches) I found out that WordPress has its own .htaccess file in the directory in which it was installed (/blog in my case). This was interfering with the .htaccess in my root directory – so I figured removing it must surely make everything start working. And it partly did. The problem now was that WordPress had been using it to serve up the right content for the fancy blog/foo/bar URLs (e.g. blog/category/foo).

A lot of tinkering later, I discovered that commenting out the redirection stuff in WordPress’s .htaccess wasn’t enough – I had to comment out the “RewriteEngine on” line as well! So maybe if I migrated the rules into the main .htaccess, it would work.

It did, with just one change – I added a condition to invoke those rewrites only for blog.nhnt11.com.

Here’s the final set of working rewrite rules:

RewriteEngine on

# Avoid www
RewriteCond %{HTTP_HOST} ^www\.nhnt11\.com$
RewriteRule ^(.*)$ http://nhnt11.com/$1 [L]

# Remove trailing slash for files
RewriteCond %{HTTP_HOST} ^nhnt11\.com$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*\.[^/]+)/$ http://nhnt11.com/$1 [R=301,L]

# nhnt11.com/blog/foo/bar -> blog.nhnt11.com/foo/bar
RewriteCond %{HTTP_HOST} ^nhnt11\.com$
RewriteRule ^blog/(.*)$ http://blog.nhnt11.com/$1 [L,QSA,R=301]

# nhnt11.com/blog -> blog.nhnt11.com
RewriteCond %{HTTP_HOST} ^nhnt11\.com$
RewriteRule ^blog$ http://blog.nhnt11.com [L,QSA,R=301]

# WordPress fancy URL stuff
RewriteCond %{HTTP_HOST} ^blog\.nhnt11\.com$
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^blog\.nhnt11\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Leave a Reply

Your email address will not be published.