• 13Nov2008
  • Leigh Profile Picture
  • New Website? Redirect traffic with 301

    Posted in Tutorials by Leigh

  • Launching a new website is exciting stuff: a re-designed, re-structured, compliant, search engine friendly website that unleashes a richer user experience upon your existing (and hopefully new) customers. This takes a lot of hard work! But just before you replace the old site with the new one, consider all the old site's pages that have already been indexed by Google, Yahoo et al. And don't forget about those all-important in-bound links!

  • The above consideration is especially important with large dynamic websites, such as e-commerce stores. It may have taken years for search engines to index all your site’s content and by changing the URLs these indexed pages would now become dead links, causing the user to see a 404 error page – not the best sales incentive. It really isn’t that hard to redirect old URLs to new ones.

    For example, your old URLs may look like this:

    • www.yoursite.co.uk/shop.php?category_id=10&product_id=20

    And your new ones like this:

    • www.yoursite.co.uk/sports-equipment/tennis-rackets.html

    For those who won’t glaze over at the site of some code, here’s a couple of examples using Apache’s mod_rewrite and php:

    RewriteCond %{QUERY_STRING} category_id=10
    RewriteRule ^/?shop\.php$ http://www.yoursite.co.uk/sports-equipment/? [R=301,L]

    In the above example (created within an .htaccess file) we are looking for any query strings (anything after shop.php?) that contain the category ID of 10, which in this example is referring to sports equipment. If the condition is met then send a 301 (permanently moved) header and rewrite the new URL to the location: /sports-equipment/. All of your indexed pages within the category will now be redirected to the sports equipment page.

    • www.yoursite.co.uk/shop.php?category_id=10&product_id=20
    • www.yoursite.co.uk/shop.php?category_id=10&product_id=30
    • www.yoursite.co.uk/shop.php?category_id=10&product_id=40

     

    The problem with the above example is that it will only take the user to the main category page and not the actual product page – it’s really just a “catch all” method. Furthermore, you might end up with writing hundreds of lines of code, depending on how may categories are within your online store. However, in many cases it’s often best to try and capture and redirect each individual indexed page to the equivalent page, within the new site. One way of doing this is illustrated in the example below.

    RewriteRule ^shop.php /redirect.php [QSA]

    In this example (also written within an .htaccess file) we are looking for any page that begins with shop.php and passing the query string (using Apache’s QSA flag) to the redirect.php page (shown below).

    $product_id = (int) (isset($_GET['product_id'])) ? $_GET['product_id'] : 0;
    $query = “SELECT product_name,category
    FROM products
    INNER JOIN categories
    ON products.category_id = categories.category_id
    WHERE product_id = ‘$product_id’ LIMIT 1”;
    $row = mysql_fetch_array($this->db->getquery($query));
    $new_url = “’Location: http://www.yoursite.co.uk/”.$row[‘category’].”/”.$row[‘product_name’].”.html’”;
    header(’HTTP/1.1 301 Moved Permanently’);
    header($new_url);
    exit;

    In the above example we have a php page which retrieves the unique product id and performs a query to lookup the related category and product name. This is then used to build the new URL path and send a 301 redirect. This is just a basic example and can be elaborated upon.

Leave a comment
  • key

Login to your control panel

Log into your hosting account using the appropriate form on the right. If you are unsure whether you are using our shared or dedicated servers please with your account name.

If you are looking for SEND or Viewyourpics, please email us with your account name and we'll send you a login URL.

Shared Server login

Dedicated Server login

Close Panel