How to do a Redirect and Why

Hey Guys,

Today I’m going to cover something that’s asked about a lot but is, in actual fact, quite easy to do. It’s the subject of redirects.

First things first though. What is a redirect and why would you want one?

Well “redirect” can refer to a few things but in this case we are referring to a server side redirect and specifically one controlled by htaccess on linux based servers.

Windows based server control redirects with IIS and while they can be easy too we will cover that in another tutorial.

A redirect is essentially a command that makes an end user or client go to somewhere other than the place they first requested, this can be for a huge range of reasons such as older pages, broken pages, site moves or more but essentially it lets you control a users behaviour.

Many designers redirect with the head command meta refresh however this is not SEO friendly, all the methods shown here will help users and spiders access your site.

The main reason people need to redirect is when they move something. If the page you move is indexed by a search engine (like Google) you should always redirect it. This is because Google will go looking for this page and if they get bounced to a 404 page, or worse a poorly done 404 page, then it could have negative implications for your site.

To check if the page you’re moving/removing is in the index use the search syntax site:www.mysite.com/movedpage.html. Putting that in Google/Yahoo/MSN will let you know if the page is there or not.

Controlling redirects on a Linux server is done by a file called .htaccess, note it does not have a name like “robots.txt” its just “.htaccess” this is called a dot file and can cause a problem for some OS’s but don’t worry you can call it what you want and rename it with your FTP client once it’s uploaded. The htaccess file should be placed in the root, alongside your index file. It can be placed elsewhere but it makes things a little more complex.

To make/edit a htaccess file use notepad or your OS’s equivalent, and simply open a new file, The first thing you will need to do is turn the redirect engine on, use the code;

##Rewrite Engine on code - MUST BE ACTIVE##
Options +FollowSymLinks
RewriteEngine on

In this file “#” are for comments.

Next you need to determine the type of redirect you want; generally you will only use two types, 301 and 302. The choice is simple, a temporary redirect is a 302, search engines will follow it but not take much note, use it when a page is down for maintenance or a section being renewed.

A 301 redirect is permanent, search engines ten to follow them and replace the old index page with the new one and most importantly, transfer all the old important things such as links and page rank to the newer version.

OK so you have you old page, your new page and you know you want a 301 redirect, what next?

A simple line of code in the htaccess is all you need.

Redirect 301 /oldpage.html http://www.mysite.com/newpage.html

That’s all you need, simple! You just need to put a new line for each redirect and test them once you upload the file. Just change the number to 301 if you only want a temporary redirect.

You can do loads more with htaccess and I will be exploring some more of the options in future posts but here are a few more snippets of code to help you along.

Redirecting a Subdirectory

##Redirect a subdirectory to temporary page##
RedirectMatch 302 ^/articles(.*)$ http://www.example.com/temporary-page.php

Redirect an Entire Site (Be careful with this one!)
##Redirect My Whole Site##
Redirect 301 / http://www.newsite.com

Redirect Whole Site to Maintenance Page
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/downtime\.html$
RewriteRule ^(.*)$ /downtime.html [R=307,L]

Comments are closed.