Blog

5 Easy SEO Redirect Rules for Newbies

Imagine: you have just renamed some of your directories, changed your domain name, changed your file extension from .php to .html or the reverse, and as a result of these changes, your Google™ rankings have suddenly dropped and so has your traffic and income…

Imagine, you had hundreds of pages indexed in Google but they are suddenly nowhere to be found, not even in the supplemental index!

Just IMAGINE! :P

[youtube]OJPWP1hNOFY[/youtube]

Sure if it was just a matter of fixing one or two files, then you could simply do a meta refresh, use a PHP redirect, or even spice up your .htaccess file a bit, as described here.

But here, I am talking about hundreds of pages! Manually redirecting each page would take you ages. Remember that for most of us, time is money. Who wants to spend so much time on doing such stupid boring work while you could just use a few shortcuts like these instead:

1. Let us say that your former directory was this:

http://yourdomain.com/directory1/directory2

But now you have shifted the contents to the following directory:

http://yourdomain.com/directory1/directory3

So what you do is backup your site’s old .htaccess file by downloading it to your local hard drive (if you don’t have a .htaccess file at all, this does not apply to you; you should CREATE one!), then open up your .htaccess file in PSPAD, and add the following code into it:

RedirectMatch 301 /directory1/directory2 http://yourdomain.com/directory1/directory3

Change all the variables like yourdomain.com, directory1, and directory2, to your specifics!

This directive will tell Google (as well as the web browser) that you have shifted your webpages to another folder! Of course, it would work for human visitors too: anyone who visits your old site would be automatically redirected to the new one!

Be sure to TEST!

If this directive does not work for you, please try by adding the following code ABOVE the directive:

RewriteEngine On

So that the complete code you should add to your .htaccess file should be:

RewriteEngine On
RedirectMatch 301 /directory1/directory2 http://yourdomain.com/directory1/directory3

Do you have WordPress, Joomla or any other CMS installed in the root of your website, and is it using a .htaccess file? Then you can simply add the following code into that .htaccess file and it should work:

RedirectMatch 301 /directory1/directory2 http://yourdomain.com/directory1/directory3

Most certainly the "RewriteEngine On" directive is already present there (especially if you are using SEO-friendly URLs, which most of us do anyway), so there is no need to add it again.

Remember that even if you are re-directing from one sub-folder to another, if you already have a .htaccess file in the root level of your site you should add the rules in THAT FILE ONLY; this is because Apache rules basically start being processed from the root/top level, much like spam filters. In other words, regardless of whatever the rules you have in the htaccess file of your subfolder, it would be overridden by the rules of the htaccess file you have in the root folder. I wasted a lot of time for not knowing this: my redirection rules never worked until I added them to the htaccess file of WordPress, because WordPress happened to be at the root of my site!

Bored already? Still 4 more redirection rules to go… :D

2. What if your old directory was
http://yourdomain.com/directory2

But now you have shifted the contents to the following directory:
http://yourdomain.com/directory3

Then the redirection rule would be:

RedirectMatch 301 /directory2 http://yourdomain.com/directory3

3. Let us go back to the redirection directive I talked about at #1, and mess with it a little bit. The original directive was:

RedirectMatch 301 /directory1/directory2 http://yourdomain.com/directory1/directory3

Now let us say that you have gotten a bit more adventurous and instead of redirecting one directory to another, you want to redirect Google/visitors based on file extensions. Let us say that all the files in your old directory had .php extensions, while in the new directory all of the files have .html extensions (but the filenames have remained the SAME!). So you can add the following directive in your .htaccess file:

RedirectMatch 301 /directory1/directory2/(.*)\.(php|html) http://yourdomain.com/directory1/directory3/$1.html

Now if the case is reverse, then try this instead:

RedirectMatch 301 /directory1/directory2/(.*)\.(php|html) http://yourdomain.com/directory1/directory3/$1.php

4. Now, what if you want both (I found out that you can either redirect from page-to-page, or from directory-to-directory using a single directive, and neither of these directives can do the other’s job; I maybe wrong though)? You not only want to redirect search engine bots from old directory to the new one, but also redirect them from the old .html file extensions to the new .php ones. Yeah I know you are getting greedy, but ya know Gordon Gecko said that greed is good! ;) (kidding). So you would simply add the rules in this order (please DO NOT reverse the order as I have found it does not work that way!):

RedirectMatch 301 /directory1/directory2/(.*)\.(php|html) http://yourdomain.com/directory1/directory3/$1.php
RedirectMatch 301 /directory1/directory2 http://yourdomain.com/directory1/directory3

5. Finally, how about the old school "domain-to-domain" redirect?

Let us say that your old domain used to be crapdomain.com, and your new domain is junkdomain.com. So you simply add the following directive to your htaccess file to redirect visitors from the old domain to the new one (please note that if you are no longer in control of the older domain then adding this directive would be useless anyway):

RewriteCond %{HTTP_HOST} ^crapdomain\.com [NC]
RewriteRule (.*) http://junkdomain.com/$1 [L,R=301]

Htaccess Best Practices:

a) Always backup your existing file before modifying it

b) Please add only ONE rule at a time to your .htaccess file and TEST it before adding another; there maybe typos in this article (and for that matter, even expert make typos anyway, and I am not even a "mod-rewrite expert" anyway) but a single typo in the htaccess file of your site could be disastrous for you!

c) If in doubt, ask your web host (my host was not much helpful in this case though;, they told me to ‘contact the developer’ instead, while this was simply a static html site built by ME that I was modifying a bit; so must *I* talk to myself then? :-) ), hunt through Google or ask Mr. Jim Morgan at the http://www.webmasterworld.com/apache! :D

I am not really making this article any more boring by adding subjects like domain canonicalization and mod-rewrite stuff which are excellently covered in these articles (but please don’t forget to post a nice comment here first, okay? ;) ):

http://www.seomoz.org/learn-seo/canonicalization
http://www.seomoz.org/learn-seo/redirection
http://www.webmasterworld.com/apache/3124709.htm

If the article does not make any sense to you at all, use Cpanel’s redirection option or the redirection plugin! :D

On the other hand if you are too afraid to edit your site’s .htaccess file I think it is time to drop your fears NOW; otherwise you will be always afraid and won’t be able to take advantage of the all the juicy stuff you could do with your htaccess file! Of course you cannot get rid of your fear right away, but you can do it with one step at a time. Let adding these simple redirection directives be your first step:

"Take the danger take the passion take the fear
On sensations do believe me lie so near;
One step two steps way beyond fear
One step takes you far;
One step two steps way beyond fear
One step way too far!" :-)

Wait, did you post a nice comment yet? ;-)

2 Comments

  1. Achmad Karno Widjaya

    Hi Arindam,
    But, what kind of comment i should say for this “advance” post? I still at CPanel basic! I have to run faster to catch this, can I? BTW, i enjoy the music Just Imagine.
    Thank you and Best Regards,
    Achmad K W

  2. stargaterich

    As usual I find your postings to be of exceptionally high quality. I don’t the post boring at all. Thanks for the timely article that touch on redirection. I was just thinking about this, particularly to clean up my directories which necessitates redirection and I am concern about the impact it has on search ranking.
    Keep up with the quality postings, I truly enjoy and benefit alot from it!