Using Apache's mod_rewrite to redirect URLs

When installing, wordpress by default deflates the blog contents into the “/blog” subdirectory of the webserver root. For example, if your website root is /var/www then wordpress will generate all its content into /var/ww/blog. Now, lets say you have a DNS account, such as my, then in order to reach the subdirectory, visitors to the site will need to type in, since the trailing /blog matches the subdirectory in which wordpress is installed.

The issue I wanted to tackle was redirecting users to the /blog subdirectory automatically. Of course, one can map the root directory of the webserver directly to the /blog, but that would have destroyed some other site functionality of mine (besides I was too lazy to change my filesystem structure šŸ˜› ). The next best, and more technically educating option was to use apache’s very own mod_rewrite. The module can basically check and compare certain parts of the HTTP packet to predefined conditions. If these conditions are met, the module can then add, substitute or replace that part of the HTTP packet. So all I needed was a mod_rewrite operation that checks the requesting HTTP host header Ā for “” and appends a “/blog” to that.

First off, mod_rewrite is by default disabled, so we need to enable the module with a trivial operation. At the command prompt, with rootĀ privileges, type: a2enmod mod_rewrite.Ā Once the module is enabled, it should appear in theĀ /etc/apache2/mods-enabled/ directory.

Now onto the actual syntax of the module. The below code must be inserted into the per-directory config file (httpd.conf) of apache:

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

Dissecting the above, the first line simply turns on the rewrite engine. The second line is the condition which must be matched in order to activate the rule. The condition is made up of two parts. The first part is which variable (or part of the HTTP packet) the module should check. In this case %{HTTP_HOST} means “check the HTTP host in the request”. The second part is what to check for, and this is made up of a regular expression. The ^(www.)?$ part reads:

“check the beginning of the string for 0 or 1 occurance of “www.”, followed by the exact string “” followed by the end of the string”

This will basically match:

The condition then triggers the rewrite rule. The third line is the rule which reads:

Look for the end of the string, if this does not finish with a “/”, then add a trailing “/”. Then add the word “blog”.

The [L] means that this is the last rule and no other rewrite rules should be applied.

There is more information regarding the mod_rewrite at the apache official website. Also, mod_rewrite is extremely useful due to its use of regular expressions, which make a very interesting and useful read, here for example.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s