.htaccess 301 Redirect Tutorial

The .htaccess file is used within an Apache server environment to set certain page request rules at the point in the http transfer protocol between the page request from the client`s browser and the server actually sending the pages. This allows for some pretty useful stuff. The main use I find is for page redirects as part of a URL rewrite engine.

The problem I`m going to have here is that the use of an .htaccess file is so extensive I haven`t the time to put down all the possible methods that can be used to redirect pages. So I`m just going to concentrate on using the Query String to perform a 301 redirect.

1. Using your favourite text editor (my preference is Dreamweaver but you can use WordPad or NotePad) create a file to go within the route of your website. Save the file and call it .htaccess. If prompted say yes to renaming the files extension.
2. Think about the layout of your server request rules. Within any URL rewrite engine there will be a rule that processes the new URL to convert it back to the old URL to get the original page information. This is the important rule and the one that has to be there. The 301 rule normally comes later and is to be used if adding a URL engine late on within the websites life cycle. It will come after the main processing rule and be used to essentially trick the spiders into thinking the old pages have been moved to the new. Doing the reverse of the main processing rule.
3. So with that in mind for a very basic .htaccess try the following (I`ll explain the different bits in a sec):

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^(.*)-pageid-(.*).html$ old_page.php?page_id=$2&%{QUERY_STRING}

RewriteCond %{QUERY_STRING} ^pageid\=46$ [NC]
RewriteRule ^(.*)$ new_page-pageid-46.html? [R=301,L]

4. OK so what we see here is a very basic set of server rules for dealing with page requests. Lets break it down line by line.
a. Options +FollowSymLinks – tells the webserver to follow symbolic links. A symbolic link is a reference link to a path that the server operating system can use to identify a hard link. A hard link is a link that points directly to a file on a server.
b. RewriteEngine On – tells the Apache server to use the mod_rewrite module. The module itself has to be installed on the webserver but normally comes with the basic install of Apache anyway.
c. RewriteBase / – lets the mod_rewrite module know that all of the rules will have no lead folder contained within the path. If set to something then all the rules specified would include that value when looking things up.
d. RewriteRule ^(.*)-pageid-(.*).html$ old_page.php?page_id=$2&%{QUERY_STRING}
The main processing rule I have discussed. In this case a pageid is taken out of the filename and placed within the query string for the old file.
So: http://www.testdomain.co.uk/test-rewrite-pageid-46.html would actually be http://www.testdomain.co.uk/old_page.php?pageid=46 . The page wouldn`t redirect but would look as if the data had been processed by the new page when in the background it is the old page doing all the work.
The &%{QUERY_STRING} at the end of the line tells the mod_rewrite to include all other query strings on the end of the old_page.php URL.
e. RewriteCond %{QUERY_STRING} ^pageid\=46$ [NC] – start of the 301 redirect rule.
Utilises the files query string. If pageid=46 is contained within the string then the rule on the next line is to be followed otherwise it is ignored.
The [NC] specifies that the rule is Not Case sensitive.
f. RewriteRule ^(.*)$ new_page-pageid-46.html? [R=301,L] – is the actual 301 redirect rule.
This takes any filename (as specified by the ^(.*)$ ) and redirects it new_page-pageid-46.html. Coupled with the previous line this will limit to files with the pageid=46 contained within the query string. It essentially does the reverse of the processing rule.
The R=301 tells the server to send a 301 redirect header to any agent that is browsing the old page. This can include client browsers or spiders alike.
The L specifies to the mod_rewrite module that this is the last rule to be followed within the .htaccess. Anything specified after this in the file is to be ignored but only if the rule is executed.

5. Now that we`re happy with that save and upload the .htaccess to the route of your website and test it. If it doesn`t work then you will probably not see any change or you will get a server 500 error.

Other .htaccess uses could include turning on and off error tracking for a certain code processor module, fixing canonical URL issues (essentially a 301 workaround similar to what`s discussed above but using HTTP_HOST as opposed to QUERY_STRING) or password protecting certain webfolders and files. It really is a useful tool that can be utilised.

Martin
Natural SEO Project Manager

  • Twitter
  • Sphinn
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • Tumblr
  • FriendFeed
  • Technorati
  • Reddit
  • Simpy
  • StumbleUpon
  • Slashdot
  • LinkedIn
  • Netvibes
  • Propeller

Leave a Reply