Digital Media Design & Marketing Blog

301 Redirection in Apache using htaccess file


Last time I checked I had a difficult time finding easy-to-digest information about the 301 canonical redirection. Why does it has to sound so complicated? Anyways, I am going to explain here each of the components of an effective htaccess 301 canonical redirection. If you are one of those people that like the complicated stuff and the higher level explanations, this post is not for you.

OK, let’s start by taking a look at the entire snippet of the canonical redirection. But before we proceed, please remember two things, one is that the .htaccess is a file that can be included in any folder under your domain and therefore from this point on we will be referring to the one located in your root directory (the same as the home page). The second thing to remember is that the htaccess won’t be available in the front side of your site (the client side), therefore either you will have to log in to your Control Panel (Cpanel for example) or you may need an ftp access to your server (which is my preferred method).

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

The first line:

“RewriteEngine on” means that if for whatever reason the engine is turned off, we would avoid several headaches trying to find what is wrong with the code. This part isn’t essential since most of the time the engine is already on.

The second line:

The Rewrite Condition. Basically refers to a set of criteria that has to be met in order to apply  the rewriting. It is composed of two parts, first the parameter and second the value. So for example in this case, the rule’s parameter is the Host requested. The value here (^yoursite.com) as you can see has a “^” sign in front of it. This sign means “at the beginning” in regular expressions. Which would mean that the Host requested needs to have yoursite.com at the beginning, otherwise don’t apply the rule. In what cases this would be the case? For example if you had subdomains configured such as: lorem.yoursite.com or lipsum.yoursite.com. Both of this subdomains wouldn’t receive the rule because the Host requested starts with something different than just yoursite.com. The final part of the second line is the [NC] directive. This directive means “no case”, so regardless of if the request was with capital letters or lower cases, still the rule would apply. For example if the request was for YOURDOMAIN.com and another one for YourDomain.com, both of them would receive the application of the rule.

The third and final line is the fun one. Up to this point nothing has been performed yet. The third line contains the rule to be applied or the “transformer”. It is composed of three sections divided by a space. The first section is the matching. What should be match from the request? In this case ^(.*)$ means: ^ (the beginning as before), then (.*) means: match (.) any character and as many times as necessary (*); finally the $ means the end of the string. So for example, if we wanted to match the first two letters of a the word “Monopoly” we could create a match by using: (..) which would return Mo.

The second section of the third line is the application of the rule. So once we matched a specific part of the request, now what? The second part is the “do what section”. So in this case, rewrite the url as: http://www.yousite.com/$1. You might be wondering: Why $1 at the end? Well the parenthesis in the first part of the third line are containing the complete request ((.*)), so we can reference that matching by using $1. And if you had several parentesis in the matching, such as (.*)ts(.*),  you could reference the first one as $1 and the second one as $2 and so forth with as many as needed.

The final portion is the modifiers. The modifiers in this case determine the kind of redirection that is taking place. The L means that if the pattern was matched then it should be considered the “last line”. And the R=301 means that the redirection should be treated as an external redirection with header code 301 (which means permanently moved to).

I hope this helps us get a better understanding of the basics of canonical redirection.

Alex Centeno MBA.

Comments are closed.