Code I Like: Using Apache's .htaccess and mod_rewrite to Redirect All Traffic to "www"

I know I was talking about HTML/CSS and JavaScript when I started this series, but I never said anything about being exclusive to the presentation layer 🙂

And heck, there’s always room for Apache when I’m talking about technology…

Anyway, a common practice in SEO circles is to 301 (301 the http status code for “moved permanently”) redirect all traffic to one version of a domain in order to maximize the power of links that flow into the site. Why? Well, search engines see http://www.example.com/link1.html and http://example.com/link1.html as two separate files, so it’s beneficial to flow links into one or the other. That way if there are two links to http://www and two links to http:// they will get together and make four. Four is more than two and in this case more is definitely better.

In the case of drunkenfist.com I redirect all traffic to www.drunkenfist.com. Here’s how I do it. I place something similar to the following code in my .htaccess file (where example.com is my domain name):

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This is common enough code, but I’ll go through it line by line so that you actually know how it works. A lot of time when people post code like this they gloss over the specifics and just let the Apache magic happen. Not me. Especially since writing this stuff forces me to understand each subject just that much more.

Here we go.

Options +FollowSymLinks

This line tells Apache to follow symbolic links. For those of you unfamiliar with unix-like file systems, links in this sense aren’t the HTML links you’re familiar with, but are instead file system links similar to, but more powerful than, Windows shortcuts. Mod_rewrite needs this option turned on to function, so depending on your configuration you may or may not need this option in your .htaccess file. My site has it on by default, but I’ve included it here for completeness’ sake.

RewriteEngine On

This line turns on Apache’s mod_rewrite. Mod_rewrite is straight voodoo.

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]

The RewriteCond directive defines a rule condition. %{HTTP_HOST} starts the rule by saying we want to look at the Apache HTTP_HOST environment variable (e.g “www.example.com” or “example.com” in our case.) The next section is the regular expression we want to test. If that regular expression resolves to true, then we want to run the next line, the RewriteRule on the requested file. It’s actually a very simple regular expression. I’ll go through it element by element just in case. ^ indicates the start of the string. This is the important part as we only want to redirect if the domain doesn’t start with “www.” So, what this does is test if the HTTP_HOST starts right in on the domain name. The next interesting part, after the domain name is the \.com$ section. The \. is simply the “.” being escaped since the “.” character has special meaning in a regular expression. The $ indicates that “com” should be at the end of the string. The [nc] at the end indicates “no case,” which makes the rule case insensitive.

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This is where the rewrite happens. The first bit (^(.*)$) is a regular expression that grabs everything (.* matches any character zero or more times- in other words anything and everything) from the beginning (^) to the end ($) of the requested URI (everything after http://example.com/) and captures it in a back reference (that’s what the () around the .* does.) The next part is the URL we’re rewriting everything to. It starts with our target domain (notice the www) followed by that back reference ($1) which contains the requested file. The last part [R=301,L] tells Apache that the Redirect is a 301 and the L tells Apache that’s the Last rule. This stops the rewriting process.

There you have it. There won’t be a ton of stuff like this in this series, but it’s always a possibility, so don’t be too surprised if more Apache stuff turns up in the future.

And please, since this isn’t my specialty by any means, correct anything that I bungle.

Posted in Web

2 thoughts on “Code I Like: Using Apache's .htaccess and mod_rewrite to Redirect All Traffic to "www"

Leave a Reply

Your email address will not be published. Required fields are marked *