2011
03.08

Today I suffered the embarrassment of referring a friend of mine to my (this) website, only for him to reply: “Dude your site is offline, it has an error!” As it turns out the site was in fact OK and reachable, but rather the issue was that I hadn’t configured Apache to redirect requests for homecomputerlab.com to resolve to www.homecomputerlab.com

As a temporary fix I simply created the following symbolic link which did the trick and resolved the issue:

sudo ln -s /etc/wordpress/config-www.homecomputerlab.com.php /etc/wordpress/config-homecomputerlab.com.php

After speaking to our in house Senior Web Developer, regarding best practices on how to circumvent this issue, he referred me back towards the Apache rewrite module which I’ve worked with and written about in past here.

Below is his email response which I couldn’t have explained better myself so I’ve posted it unedited here:

There is a better way so you don’t need to explicitly define the site, it users an apache variable like {HTTP_??}, also the rewrite rule should use {HTTP_HOST} to cater for https connections, I never needed to expand it so left it as it is here…

# force www
RewriteCond %{HTTP_HOST} ^([a-z.]+)?homecomputerlab\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www\.homecomputerlab\.com%{REQUEST_URI} [L,R=301]

To explain it a little

  • RewriteCond means the following condition must be met, the conditions need to be met to process the next ‘RewriteRule’, otherwise it’ll skip over it.
  • We are looking for the alias before the hostname (NC is non case check)
  • Then we check if the part before the hostname is not equal to ‘www’
  • Then we redirect the user to the same address with ‘www’, L means last rule and R=301 means permanently moved to this address
  • Remember with the last rule sequence, you are sending the user to another url on your site, when they request this new url they will re-run the rule set picking up further rules after this rule should you wish to add anything after this.

 

– Kevin.

 

2 comments so far

Add Your Comment
  1. Pretty! This was an incredibly wonderful post. Many thanks
    for providing these details.

  2. Marvelous, what a webpage it is! This blog gives useful facts to us, keep it up.