How to move pages permanently with .htaccess
Web sites don't last forever. In the online world, a site that is a mere three years old will likely be starting to look tired in need of a visual refreshing. Yes, web fashions really do move that fast. And the upshot is that web designers are regularly having to make significant changes to existing web sites. If all they're doing is changing the appearance, that's usually not a huge problem, but what if pages need to be moved around? How do you manage the problem of external sites linking to pages that are no longer there?
An overview of options
Web developers are actually quite spoiled for choice when it comes to choosing a suitable redirection method, though some techniques are more effective than others. Here are just a few for comparison (there are others, but this isn't intended to be an exhaustive list).
The meta refresh
<meta http-equiv="refresh" content="0;url=http://www.example.com" />
This code, when inserted in the <head> ... </head> area of an html page, will forward the visitor to another page. It's quick and easy to set up, and works like a charm. The only problem here is that the page has to actually exist in the first place; if you have a lot of pages to redirect you will end up with a lot of mostly-empty files dotted around, which will be a nightmare to keep track of and is not particularly efficient. For occasional redirects, this is fine, but for anything more substantial it just doesn't cut the mustard.
PHP redirect via header(...)
<?php header('Location: http://www.example.com'); ?>
If you are using PHP, you can redirect the visitor's browser to a different URL with the above code. Note that the full URL is needed for this to work; a relative URL probably won't have the desired effect. This is often faster than using a meta tag, and doesn't require the page to be rendered by the browser before the redirect. However, like the meta refresh method, this does still require the page to exist in the first place.
.htaccess Redirect
Redirect some/folder/myfile.html http://www.example.com/newlocation/myfile.html
This code, when added to an .htaccess file, should have the effect of redirecting an incoming URL to somewhere different quickly and efficiently. Because you can have as many of these redirects as you like in one .htaccess file it makes it much easier to manage, and saves having redirect files dotted around your web space. Beautiful.
Unfortunately, it's not without its flaws, as I discovered when I recently upgraded this site. I am using the MODx Content Management System to look after the site, which uses the .htaccess file to transform messy and unhelpful URLs like /index.php?id=123 into something meaningful like /my-nice-page.html. If you're using Friendly URLs (FURLs) you'll probably find that it conflicts with the Redirect directive, and non-existent files will end up in a 404 error, despite your intended redirects.
.htaccess RewriteRule
RewriteRule ^some/folder/myoldfile.html newlocation/myfile.html [L,NE,R=301]
On the surface of things, this ought to do the same as the Redirect directive, but with the main difference that it doesn't conflict with any FURL RewriteRules that may also be in operation. It also more adequately serves up the 301 'Permanently Moved' header, which is always useful, especially when trying to persuade search engines not to list your old pages any more. Both locations can be relative, rather than absolute URLs, and again there is the clear advantage of having everything in the same place.
For example, on the previous version of this web site my article on how to record sermons was to be found at www.matthewdawkins.co.uk/resources/other/how-to-record-sermons.html. With the revised site structure, that URL would no longer work, but if you click on that link you'll see that the URL is updated almost as soon as it's entered and you'll be taken to the actual page. This is all done via the .htaccess file, which recognises the incoming URL and points you in the right direction. Nice.
Other uses
These techniques can be used for more than just making sure old links go to the right place. You may find yourself in a position where your site structure puts everything in a particular format or location, such as www.example.com/pages/some-category/1/my-page.html, which is a bit of a mouthful! You can use .htaccess rules to create a shortcut to that page, something short and snappy like www.example.com/my-page.html. Assuming /my-page.html doesn't actually exist in that location, people going to that shorter URL will be automatically redirected to the actual location of the page. Pretty neat, huh?
Reference
Check out these sites for more information on using the above techniques:
This post was originally published on www.matthewdawkins.co.uk.
Copyright © Matthew Dawkins 2009
About Matthew Dawkins
Matthew is a web designer based in Somerset, UK. He has a passion for CSS and design, and runs his own web design business.







Write a comment