Website Redirects on LAMP Hosting

Posted November 26th, 2009 by Barnaby Knowles in Website Development

Google Buzz

You might want to redirect web pages or even your entire website for one reason or another. Perhaps you changed the name or location of a web page, you changed from HTML to PHP file extensions, you changed your domain name… the list goes on. Luckily there are many different ways in which you can set up redirects in a LAMP (Linux, Apache, MySQL, PHP) web hosting environment.

When to Redirect?

There are lots of reasons why your web page URL may change.

  • You change the page URL for better SEO
  • You change from using static HTML pages to dynamic PHP pages
  • You simply reorganise your directory/page structure
  • You change your domain name
  • You remove a page but want to forward any visitors to a different page

These are just a few of the situations in which you might want to redirect web pages.

Why Redirect?

If you’re changing the URL for SEO, technical or organisational reasons you will want any visitors arriving at the old URL to be seamlessly redirected to the new URL. If you remove an old page you won’t want to risk losing the traffic landing on that page by issuing a “404 Not Found” error, so you can redirect visitors to a similar page or to your home page instead.

Google Webmaster Tools will also list any pages that return a “404 Not Found” error in its “Crawl errors” section. Therefore redirecting any pages that you remove from your website will result in fewer crawl errors in your Google account, which can only be a good thing for your website!

How to Redirect?

As a webmaster with LAMP hosting you have several ways of redirecting your visitors:

  • HTML meta tags
  • PHP script
  • Apache .htaccess file

HTML Meta Tags

One early way of redirecting users was to use the meta refresh tag. This element would instruct the web browser to automatically refresh the current web page after a given time interval. An alternative URL can be included in the element, which tells the browser to fetch this URL when the content is refreshed. By setting the refresh interval to a very low value (i.e. zero) a URL redirect is achieved:

<meta http-equiv="refresh" content="0; url=http://example.com/" />

This method of redirection is not recommended for various reasons:

  • It may “break” the “Back” button, as the browser will return to the redirecting page and get redirected again.
  • Search engines are not keen on it.
  • W3C deprecated it

PHP Script

PHP allows us to add headers to the server response. This means that we can modify the status code that is returned when a visitor requests the page that we want to redirect. That will send the visitor directly to the new page, which is much better than the meta refresh option.

The two main HTTP status codes that allow us to redirect a visitor are “301 Moved Permanently” and “302 Found”. A status code of “302 Found” means that the requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests.

This type of redirect can be implemented with a couple of lines of PHP code:

<?php
header('Location: http://www.example.com');
die();
?>

Strictly, this type of redirect should only be used when the content has moved temporarily. Historically has also caused issues for search engines and is therefore not recommended.

A status code of “301 Moved Permanently” means that the requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. This is the correct way to redirect a visitor and can be implemented easily with a few extra lines of PHP code:

<?php
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.example.com');
die();
?>

(Note the extra header to specify that this is a permanent redirection with a 301 status code.)

Apache .htaccess File

The final and most complex but incredibly flexible way of redirecting website visitors is to make use of Apache’s .htaccess capabilities. A .htaccess (hypertext access) file is a directory-level configuration file that allows for decentralised management of web server configuration and one of the things that it lets us control is URL rewriting.

(Note that in order to use .htaccess to rewrite URLs or redirect visitors you’ll need the Apache mod_rewrite installed. It’s an extremely common module so the chances are that you will have it installed by default.)

URL rewriting with .htaccess is an incredibly powerful and complex feature. We’re going to concentrate on just one aspect: redirection.

A .htaccess file is simply a text file that resides in a directory within your website and affects that directory and all directories below it (unless explicitly disabled in the server configuration). A .htaccess file in the root directory would therefore affect all other directories in your web space.

First the rewrite engine must be turned on with the following line:

RewriteEngine On

Your redirect code follows this statement.

One simple way of redirecting pages to new locations is to list each old URL with the new URL and tell Apache to issue a 301 header:

Redirect 301 /old-page.htm http://www.example.com/new-page.htm
Redirect 301 /old-html-page.htm http://www.example.com/new-php-page.php

Note that the domain name does not appear in the old URL, it’s a relative path and must begin with a forward slash. The new URL is an absolute URL.

A more complex and powerful way to redirect URLs is to use regular expressions. This allows you to match patterns in the URL and redirect visitors based on those patterns. You might have a batch of URLs that want redirecting to new locations and rather than entering all of them separately you can set up a regular expression to match all of them and redirect them appropriately.

The scope of regular expressions is beyond this article but some simple redirects can be set up as follows (lines beginning with hash symbols – # – are comments):

# Redirect any request to *.htm pages to the same PHP page name
RewriteRule ^(.*)\.htm$ /$1.php [R=301,L]

# Redirect an old directory to a new one
RewriteRule ^old-dir/$ /new-dir/ [R=301,L]

At the end of the rule, R=301 signifies that the redirect should be sent with a 301 HTTP status code and L signifies that this is the last rule to follow for this request.

You can combine both forms of redirect, so your .htaccess file might look something like this:

RewriteEngine On

Redirect 301 /old-page.htm http://www.example.com/new-page.htm
Redirect 301 /old-html-page.htm http://www.example.com/new-php-page.php

# Redirect any request to *.htm pages to the same PHP page name
RewriteRule ^(.*)\.htm$ /$1.php [R=301,L]

# Redirect an old directory to a new one
RewriteRule ^old-dir/$ /new-dir/ [R=301,L]

There is so much that can be done with regular expressions that it deserves an article of its own, if not a series of articles, so we’ll leave it there for now!

Conclusion

Of all the different ways that exist to redirect a visitor, the most powerful and flexible is using regular expressions in your .htaccess file. If you use a .htaccess file you can delete those old HTML or PHP files that would contain the earlier redirect code examples, which will de-clutter your web space and reduce the amount of disk space that your website requires. It also keeps all of your redirect code tidily in one location.

  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • FriendFeed
  • LinkedIn
  • MySpace
  • Ping.fm
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter



Leave a Reply

 
Follow us on twitter! View Our Digg Profile!
Browse Our YouTube Channel! Check Out Our Delicious Bookmarks!
Connect With Us On LinkedIn! Find us on Facebook
Make Child Poverty History
© 2009 RAM. All rights reserved. Built and Powered by WSI. | Sitemap
Website Development and Online Marketing for Huddersfield, Leeds, Manchester, Sheffield & West Yorkshire

WSI Internet Consulting, The Media Centre, 7 Northumberland Street, Huddersfield, HD1 1RL
Registered in England No. 4968860, Bridge End House, Park Mount Avenue, Baildon, BD17 6DS