Creating SEO friendly URL’s using .htaccess

Creating SEO friendly URL’s using .htaccess

Search engines follow their own set of rules, each one different and rarely do two search engines have the same algorithyms for how they display search results. This includes the major players like Google, MSN, Yahoo, and so on. But to healp make your website URL’s more friendly to most of them, we can deal with an issue of pages that query a database and give us the ‘?’ followed by a series of variables.

So if you have a website that has a database of products, lets use Cheese for example, in the database the products would be identified by an id variable. For one of our products we will use Gouda, which has an in of product 66. When searching on your website or going through products, the existing site displays Gouda based off the id, for example:

http://www.mycheesewebsite.com/product.php?id=66

Of course looking at the URL, and when indexing (search engines) the above example, there is no way to tell what the product is. Also, some search engines will not spider beyond the ?. So how then, do we get the search engine bots to spider the page so it ends up being indexed?

Simple, we use .htaccess and the RewriteEngine protocal to change the URL to something more Search Engine friendly/optimised or SEO, so our webpage looks like this:

http://www.mycheesewebsite.com/gouda-cheese.html

Here is how we accomplish the. There are two steps. One requires modification of your .htaccess file. ( root directory – public_hml ) if for your entire website, or a sub directory ( public_html/cheesestore )

  1. Step One: Open the .htaccess file and add the following code:

 

RewriteEngine OnRewriteRule ^(.*)_pr(.*).html product.php?id=$2

  1. What this will do is tell the server that gouda-cheese.html is really product.php?id=66. You see we are not really changing the file names, .htaccess just does it for us.The $2 refers to the second instance. PHP .
  2. All you need to do now is modify the script that creates the link to the product to display the product name and ID rather than just the ID. For example. You will need to alter all code in RED to match your tables.

 <?php

$product = msyql_fetch_assoc(MYSQL RESOURCE YOU CREATED PREVIOUSLY);echo ‘<a href=”‘.convertProductName($product['product_name']). 

’_pr’.$product['product_id'].’”>’.

$product['product_name'].’</a>’;

function convertProductName($p_name){

// Takes a string, and returns the same string but with all

// the spaces converted to hypens etc.

}

?>

Test in browser. If you run into problems, check that .htaccess file is saved as a unicode file.



Leave a Reply

© Copyright Sellopolis (Formerly Prozilla Turnkey Websites) 2002-2011. All rights reserved. | Original Theme by Elegant Themes. Modified by Sellopolis
Calgary Painters | Airdrie Painters | Calgary Corporate Catering