The 2 best ways to Redirect a URL


Image courtesy of Darwin Bell
It’s happened to us all. One day you’re merrily surfing along, you click a link, and Bam, you get a page not found error. Why, what happened? Well, chances are the link you followed used to resolve to a legitimate page, but the page has now been taken down or moved to a new location. Couldn’t it have left a forwarding address? Well actually, yes it could (and should) have.

What’s so bad about broken links?

Firstly, it creates a very bad impression for your site’s visitors. Someone who is presented with a page not found message may, if you’re lucky, use your search facility to try and track down what he was looking for. But chances are he’ll simply move on to the next site and you’ll have lost him.

Secondly, consider what happens when a search engine bot encounters that 404 header – the url will be removed from the index, and consequently all the traffic that search engine may have sent your way disappears. Considering how hard it can be to acquire backlinks in the first place, it’s very careless to let that happen.

Sometimes moving pages is unavoidable

You should do your level best to maintain the integrity of your site map, which is made up of all your publically accessible urls. After all, there’s a reason they’re called permalinks – they’re supposed to be permanent! But sometimes it is necessary to shuffle things around, for example when a page is no longer relevent because you no longer offer a particular service. What to do then?

This is where url redirection comes into play. There are actually many ways of redirecting a url, but I want to cover what I consider to be the two best ones (because they’re browser-independent and work equally well for humans and search engine bots):

Redirection using .htaccess

The .htaccess file in Apache web servers (and most sites in the world are served by Apache) is a very powerful way of over-riding all kinds of configuration settings. I don’t claim to be an expert on all the things you can do with it, but redirecting a url is very easy.

Note: because your .htaccess file is so critical, it’s advisable to take a backup before modifying it. That way, if you do get yourself into trouble, you simply have to restore the previous version to fix things.

Let’s say you want to redirect http://domain.com/old.html to http://domain.com/new.html (obviously substitute your specific domain and page names where appropriate). Simply add the following to your .htaccess file:

RewriteEngine On
Redirect 301 /old.html http://domain.com/new.html

Note that if you’re using WordPress, your .htaccess file should already include the RewriteEngine directive, so you will only need to add the line containing the actual redirection command.

Save the file, then test that it works by navigating to http://domain.com/old.html – you should end up at http://domain.com/new.html.

Redirection using php

This method is commonly used to hide affiliate links (the rationale is that certain types of visitors are less likely to click links they perceive to be affilate-related).

Let’s say you want to mask http://affiliate.com?id=aff-code. Simply create a php file with a more innocuous name – I’ll call mine innocuous.php – and include the following in it (again substituting your specific affiliate link):

<?php
header("Location: http://affiliate.com?id=aff-code");
?>

Now, instead of embedding http://affiliate.com?id=aff-code in your site’s pages, you use http://domain.com/innocuous.php. This also puts the affiliate link in a single place, so if you should ever need to change it, you only have to edit this one file, rather than find all the places it’s referenced throughout your site.

One thing to note about this approach is that you will end up with a single php file for each url you redirect. Whereas with the first approach you still only have a single .htaccess file, no matter how many urls you are redirecting. I personally don’t see this as a problem though.

So there you have it: two top ways to redirect urls, and eliminate broken links from your site forever!

No related posts.

6 Responses to “The 2 best ways to Redirect a URL”


  1. Gravatar of htaccess redirect 1 htaccess redirect

    Really I think that using PHP redirectional scripts are a better approach for affiliate links. They don’t require a seperate web page for the PHP redirect code. This in my eyes is more search engine friendly then actually redirecting a page via PHP, these pages tend to get indexed sometimes even though they redirect.

  2. Gravatar of Rodney Smith 2 Rodney Smith

    Sorry, I don’t understand what you’re getting at – care to elaborate?

  3. Gravatar of natural pet care 3 natural pet care

    Nice work Rodney. i had many many trouble with moving pages and redirecting them. but this looks like a better solution from all the research. Came across some not so useful articles. But this hits the spot. I’ll try it out tonight. Thanks again. Appreciate the work.

  4. Gravatar of permanent one way links 4 permanent one way links

    I agree with htaccess redirect. Using PHP re-direct scripts is somewhat more efficient and better. But really appreciate your effort to mention both. Thanks a lot.

  1. 1 ThatsBlog.com » Blog Archive » Blog Carnival: Blogging: Eighteenth Edition
  2. 2 Easiest way to manage broken links at Hippo Web Solutions
Comments are currently closed.