Search Engine Optimization is all the buzz these days.  Early days of the Internet, websites were all about the information and content, along with really low quality gif clip art.  Early search engine companies found it reasonably easy  write ‘bots’ or spiders to parse the website, analyse the content and make it searchable.  As bandwidth increased, multimedia found its way to the internet, and soon followed by ads.  There was a time for about two years, when all web development was about Adobe Flash. To the point that I even witnesses entire websites being on large flash file.  Flash would make your website look good, but there was no content for an outside source to analyze your website.

So, now Google, Bing and others allow the web developer to encode information about the website within the website.  In the olden times, this was just content.  Now with SEO, you develop your content, and then encode your content, marketing keywords within the html of the site.  This is all well and good, but it makes it more important that your website design to condiser this prior to the start of development.

This brings us to Friendly URL’s.  Friendly URL’s are nothing more that more human readable web page names.  Prior to friendly URL, it would be very common to see a URL like:

 

http://somedomainname.com/book/details.php?cat=3&book=15

 

The server would then pull the query string value of ‘3’ from the URL, look up information from a database, and displays the results.  It still works the same way.  However, with Friendly URL’s, the query string could be:

 

http://somedomainname.com/book/details/classics/The_Catcher_in_the_Rye.php

 

Setting up the URL like this accomplishes several things.  First, it just looks a lot better to your target audience, which is people.  Second, since the web page is technically named after something, and not containing an integer and corresponding id, this URL gives the search engine something else to consider when evaluating your website.  So, now the question becomes how to make this work with a database driven website.

 

Web server:

The first step is to configure your web server to ignore the URL requested and serve the page you want the user to see.  This is done in apache, my web server of choice, using the mod_rewrite module.  I won’t go into getting mod_write installed as this is another discussion, however you must enable it.   To do this, add the following directive to your apache configuration or .htaccess file:

RewriteEngine On

With Rewrite enabled, you can set up your rules with an .htaccess file in the directory which contains the file that does the work, which in my example is the /book/ directory.

RewriteEngine On
RewriteRule ^details/(.*)/(.*).php /details.php?cat=$1&book=$2

This directive will tell apache to look at header URL and if the URL starts with the value details/ and in the format of details/(.*)/(.*).php where (.*) is simply a wildcard, to send that request to details.php with the query string appended to the back of the page.  The wildcard values are appending into the new URL using $1 for the first, $2 for the second and so on.

 

PHP Code:

The php code to support this is rather straight forward with a few caveats.  If you are pulling values from a database, it is very common for the values to contains spaces.  If you leave the spaces, when the URL is posted, it will change the spaces to %20, which is ugly in appearance, and defeats our purposes.  Additionally, should someone copy and paste you URL into other website, email, text, social network, etc… the odds are really good that when this happens, the URL will break on space and most likely break your webpage.

To solve this problem, I replace all of the empty spaces with and underscore.  This prevents the URL from breaking and maintains our readability.  To do this in php our example may look like:

 

<?php

$cat = str_replace(” “,”_”,$rs[‘categoryname’]);

$book = str_replace(” “,”_”,$rs[‘bookname’]);

echo ‘‘.$rs[‘bookname’].’‘;

?>

This code snippet will encode the data in  a query string into the URL itself.  The final step is decode the values into variables that you can use.  The first is to assign the entire URL into an string.  Then you break the string into an array using the ‘/’ as a delimter.

<?php

$bookinfo = $_SERVER[“REQUEST_URI”];

$pieces = explode(“/”, $bookinfo);

?>

Once the variables are in an array, you can assign columns in the array into specific variables:

<?php

$statename = str_replace(‘_’,’ ‘,$pieces[3]);
$areaname= substr(str_replace(‘_’,’ ‘,$pieces[4]),0,-4);

?>

Now, do something with them, and don’t forget to replace the underscores to pull your original values out.  The last variable is further parsed to remove the ‘.php’ from the end of the URL.

Leave a Reply

Your email address will not be published. Required fields are marked *