Creating SEO friendly URLs using .htaccess
If you have an existing site that you want optimize for search engines, one way is to use the .htaccess file in your root directory - normally in /public_html/.htaccess
Say your existing site displays a product based off a product ID, for example:
http://www.mycoolwebsite.com/product.php?product_id=12
and you would prefer to have the url look like:
http://www.mycoolwebsite.com/really-cool-usb-missle-launcher_pr12.html
- Open the .htaccess file and add the following code:
RewriteEngine On
RewriteRule ^(.*)_pr(.*).html product.php?product_id=$2What this will do is convert really-cool-usb-missle-launcher_pr12.html back to product.php?product_id=12.
The $2 refers to the second instance (.*).
You may run into problems when saving the file, it needs to be saved as a unicode file.
- 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:
<?php
$product = msyql_fetch_assoc(SOME MYSQL RESOURCE THAT 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.
}
?>
That’s pretty much it, email me at roger@feedthefridge.com for more info or if these instructions need modifying.
Filed under: Dreamweaver, HTML, SEO