Using Search Engine Friendly URLs for your Dynamic Pages
Monday, July 14th, 2008 at 12:05pm, in SEO, written by Stefan Ashwell
"Search Engine Friendly URLs" or "User Friendly URLs" are a much better way of constructing your URLs for dynamic pages. In this article I will discuss some basic ways of doing this, with a little help from mod_rewrite.
In my other recent article about Search Engine Friendly URLs, I discussed how you can create a properly formatted string from your page or article titles. You can combine this with the following tips.
The idea is to be using nicely formatted, meaningful URLs like below:
http://www.website.com/search-engine-friendly-urls/
If you can imagine, you have a database driven website, with a number of pages stored within - one of which is called Search Engine Friendly URLs. Usually your URL to get to this page might look like this:
http://www.website.com/page.php?pageid=5
The PHP script page.php would query the database for the row with the ID of '5'. I won't go into the full working of how this is usually done, but concentrate on the SEO side of things for this article.
Mod Rewrite
Note: I'm only going to cover some simple Mod rewrite here, but there's a nice cheat sheet here if you want to learn more.
Mod Rewrite is an Apache module that allows you to rewrite URLs. In other words you can "mask" a horrible, unfriendly URL, with a nice readable one, and still have it get the information out of your database. You will need to create a .htaccess file and put it in the root directory of your site. To begin with the file will need the following in it:
RewriteEngine On
Quite simply, this lets us use Mod Rewrite. OK, now let's do something useful...
RewriteRule ^search-engine-friendly-urls/$ page.php?pageid=5 [L]
The above line will redirect the first section (search-engine-friendly-urls/) to the page listed second (page.php?pageid=5). Putting a $ at the end of the first part means if anything else is entered after what is listed, it will not perform this rewrite (so it will only ever rewrite the page search-engine-friendly-urls/). Right at the end of the line there is a [L]. This tells the server to stop if it matches this line. You might have a .htaccess file with a number of RewriteRules in it, but once it's matched this one it can stop.
Your PHP script shouldn't need to change, the only thing you may need to change is any link references or references to css or similar files in your HTML. As your browser now believes you are a directory down from where your page actually is.
Getting more dynamic
The above is all very well and good if you only have a few pages that will never change, however if you have a CMS where pages could be added on a regular basis, or page names that could change or be removed altogether, managing this .htaccess file could become a nightmare. Do not fear however, as you can be more dynamic with Mod Rewrite.
The first step is to create a new field in our database to store the Search Engine Friendly optimised name in. In our example the field would include 'search-engine-friendly-urls'.
RewriteRule ^page/(.*)/$ page.php?pageurl=$1 [L]
In the RewriteRule above (.*) will capture any characters and put them in a variable $1 that is used later. Therefore anything that is entered in the URL after page/ and before / will be passed as the pageurl GET variable. You can then use this to retrieve the database row with that identifier.
I hope you've found this article useful. As I said earlier the PHP workings are for another article which you may find on here at some point. If you have any comments don't hesitate to leave one!

















There are no comments on this article yet
Please log in to post a comment about this article.