Some .httaccess Settings to Separate my Blogs

Until recently, if a visitor went to https://ianhocking.com, they’d be redirected to my WordPress blog of several years called This Writing Life. I now want to move this to one side this so I can give prominence to my new programmer blog. However, I’d been having trouble keeping the two blogs separate.

If a person went to https://ianhocking.com, they’d see my programmer blog. But if they requested a page that did not exist, they’d always get directed to my writing blog. Not only that, the writing blog’s search was broken (as was any post preview within WordPress dashboard itself, caused by the preview sending an ‘empty’ URL to my domain, which would be redirected to my programming blog).

Table 1: Desired behaviour
URL Type Behaviour
https://ianhocking.com exists Redirect to programmer blog
https://ianhocking.com/something does not exist Redirect to programmer blog
https://ianhocking.com/2013/06/22/writing-a-novel-using-markdown/ does exist Redirect to This Writing Life blog

I had a frustrating time with .htaccess files—the desired behaviour of sending this to either the programming blog or the writing blog depending on the URL succeeded intermittently, basically because I was having trouble fully understanding the rewrite rules that WordPress had applied to the .htaccess1 file. In a nutshell, WordPress alters this file for the root folder (in my case, ianhocking.com) and then intercepts any URLs that don’t exist as files on the server. Because permalink URLs are often abritrary, and don’t directly map onto the server’s directory structure, this is a good way of dealing with permalinks.

Here is the relevant portion of my .htaccess file, with explanations of what each of these lines does based on a handy-dandy page over at Glenn Messersmith’s site.

# BEGIN WordPress
 <IfModule mod_rewrite.c>             # Is the server able to do URL rewrites?
                                      # Begin block
 RewriteEngine On                     # Start rewriting URLs
 RewriteBase /                        # Base URL relative to the WordPress files
 RewriteRule ^index\.php$ - [L]       # If requested file is index.php,
                                      # take user there and stop processing rules
 RewriteCond %{REQUEST_FILENAME} !-f  # Does the requested file not exist?
 RewriteCond %{REQUEST_FILENAME} !-d  # Or does the directory not exist?
 RewriteRule . /index.php [L]         # Otherwise replace request with index.php
                                      # and go there (WordPress will still know
                                      # the original request URL)
 </IfModule>                          # End block
# END WordPress

Elsewhere in the .htaccess file, I set the DirectoryIndex (i.e. the place you’re taken if only the directory is requested) to my new blog’s 404 page:

DirectoryIndex /prog/index.htm /prog/index.html prog/index.php
ErrorDocument 404 /prog/404.html

The Fix

My solution was to move the blog to a subdirectory, twl, so it’s now accessed at https://ianhocking.com/twl/. I updated the permalinks settings within WordPress itself (Settings > General Settings > Site Address (URL): https://ianhocking.com/twl), so that WordPress is aware that twl should be part of the permalink address.

This meant I could the tweak the .htaccess file (in both the site root and the WordPress directory) with the effect that WordPress would only intercept non-existent files or directories if they were prefixed by twl. This allows me to separate the blogs more senisbly.

RewriteRule ^twl.* /index.php [L]

One last thing. I had worried that this would break all the incoming links to my writing blog. However, it happens to be the case that these posts are date-based, and thus always begin with a number. Another RewriteRule to detect these and throw to the WordPress index.php page would fix it. So here is my final .htaccess file (again, one version for the root of the domain and another for the WordPress directory itself).

# BEGIN WordPress
 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^twl.* /index.php
 RewriteRule ^[0-9].* /index.php [L]
 </IfModule>
# END WordPress

  1. A file used by the Apache web server to decide how pages are served in a given directory. [return]
comments powered by Disqus