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 ddilittleitaly.dyndns.org, then in order to reach the subdirectory, visitors to the site will need to type in ddilittleitaly.dyndns.org/blog, 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 “ddilittleitaly.dyndns.org” 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.)?mydomainhere.com$
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.)?mydomainhere.com$ part reads:
“check the beginning of the string for 0 or 1 occurance of “www.”, followed by the exact string “mydomainhere.com” followed by the end of the string”
This will basically match:
mydomainhere.com
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.