Create SEO Friendly URLs With Mod Rewrite and PHP – Part II

More than a year back I had written a tutorial on creating SEO Friendly URLs with Mod Rewrite and PHP, where I had explained the basic SEO concepts of rewriting URLs to make them SEO friendly.

Though It has been over a year, numerous emails and comments have dropped in for me to write the second part of the series, so here it is :-)

In the first part I had explained the basic changes you would require to do in the .htaccess file to create SEO friendly URLs, the post explained how you can internally redirect a URL to any PHP page that is stored on your web server.

In this post I will explain how you need to modify the PHP files so that it will generate the SEO friendly links for your website.

The original URL we were dealing with would create URLs as this one, here is a example PHP code of that generates this URL. I am leaving the Database connection code out of the example to keep the examples short.

   1: <?php
   2: // Retrieve all products from the table
   3: $result = mysql_query("SELECT product_id, product_title, category_id  FROM products") or die(mysql_error());  
   5: //fetch the result array
   6: $products = mysql_fetch_array( $result );
   8: //iterate the products and create links
   9: foreach($products as $product) {
  10:     $product_id = $product['product_id'];
  11:     $product_title = $product['product_title'];
  12:     $category_id = $product['category_id'];
  13:     //write the URL as
  14:     echo "<a href='$product_id&amp;categoryid=$category_id'>$product_title</a>";
  15: }
  16: ?>

In order to generate SEO friendly links you will have to change the way you write the links to the page, the changes are pretty minor and here is the example code for writing the SEO friendly URLs on your pages with PHP.

   1: <?php
   3: //iterate the products and create links
   4: foreach($products as $product) {
   5:     $product_id = $product['product_id'];
   6:     $product_title = $product['product_title'];
   7:     $category_id = $product['category_id'];
   8:     //write the URL as
   9:     $product_title_url = gen_seo_friendly_titles($product_title)
  10:     echo "<a href='$product_id/$category_id/$product_title_url.html'>$product_title</a>";
  11: }
  14: function gen_seo_friendly_titles($title) {
  15:     $replace_what = array('  ', ' - ', ' ',    ', ', ',');
  16:     $replace_with = array(' ', '-', '-', ',', '-');
  18:     $title = tolowercase($title);
  19:     $title = str_replace($replace_what, $replace_with, $title);
  20:     return $title;
  22: }
  24: ?>

In the code example, I changed the URL structure to match what we created in the .htaccess file, I am changing the product title to SEO friendly version by calling the method gen_seo_friendly_titles which takes the product title as a parameter.

The gen_seo_friendly_titles method basically lower cases the product name and replaces spaces and dashes to form a proper URL. You can use your own replacements in the method as you see it fit.

Also as you can see I am not directly manipulating the $product_title itself and saving it in another variable called $product_title_url, since we will be using the $product_title as the anchor text and would want it to remain as it is.

This concludes the series of creating SEO friendly URLs with Mod Rewrite and PHP, hopefully this should answer the many emails and comments about where the second part is :-), feel free to drop your question in the comments.

Published by

Keith Dsouza

I am the editor-in-chief and owner of Techie Buzz. I love coding and have contributed to several open source projects in the past. You can know more about me and my projects by visiting my Personal Website. I am also a social networking enthusiast and can be found active on twitter, you can follow Keith on twitter @keithdsouza. You can click on my name to visit my Google+ profile.