HOME  |  AJAX  |  SOLUTIONS  |  TECHNOLOGIES  |  E-COMMERCE  |  ABOUT US  |  JOBS
My URL Rewriting
URL Rewriting can be tricky, because you either have to plan for it upfront, or try and shoe-horn it in later.  I have come up with a methodolgy that seems to work well for me that I will share.

Most of the websites that I create use a menu ID to specify what content to render on a page.  This site actually uses a menu ID and a page ID to determine what content to display on the page.  The reason for this is because Menus don't have to have a one-to-one mapping with the content they represent.  You can have more than one menu point to the same page.

So that's why this site has the weird numbers in the URL rewrites after the "Friendly Name" (i.e. Friendly_URL_Rewriting-2_234_261_305.html)

What I have done on some other sites that I created was to give each menu a "Friendly Name" that is used to identify the menu and page that it represents.  Then you can use this "Friendly Name" to create a keyword packed URL that also has the benefits of appearing static to the browsers and SE's.

What you end up having is that most dynamically created websites use either a page ID or menu ID to determine what content to render to the user like:

index.php?page=1 or index.php?menu=1

With the URL rewrites the main goal is to remove the QueryString from the URL because search engines don't like to follow URLs with QueryStrings attached (Then tend to get stuck on sites that have search features)

So our first goal is to get rid of that pesky QueryString with the URL rewrite.  So instead of displaying this URL with the QueryString we will rewrite it like:

index-1.php

Now with our WebServer gets a request with this format, we will rewrite it with a regular expression to:

index.php?page=1

Our regular expression might look something like:

^/index-/(.*)\.php /index.php?page=$1

This probably looks a little confusing if you haven't worked with regular expressions before.  Regular expressions are extreamly powerful, and there an area of programming that I think every code out there could work on.  For a little more on Regular Expressions, check out my RegEx section.

As long as we're going to rewrite our URL's before they are processed by our severside code, we might as well change the .php extension to .html  This further provides the illusion that our page is static as well as hiding our underlying architecture from users.  So now instead of writing our our URLs like:

index-1.php

We will write them like

index-1.html

But while we're putting this much effort into it, why not pack some keywords into our URL.  There is nothing to say that a menu / page has to be identified by a number.  We could just as easily use a unique string to identify the menu / page.  This will take a little bit of error checking in our admin section, but is certainly doable.

So what I like to do is have the Friendly Name be the static page representation in the URL, and provide a marker as a subdirectory path in the URL to let our Rewriter know this is going to be a Friendly URL path and to rewrite the URL.  This way, we can still have static pages and other pages on our site that don't get effected by the URL rewrite. 

This site was on of my first cracks at URL rewriting, so it seems a little silly.  You can seen in the URL that I have a sub dir of /fr/.  To me when I created this, I was thinking friendly name (hints the fr).  Now looking at the URL, it appers to be a French version of the site.  It probably would have made more sense to put /en/ instead.  Either way, it is just a marker so I know to rewrite the URL when it arrives at the WebServer.

Now anytime something arrives at the server with the /fr/ subdirectory in the URL, I know to rewrite it.  Our URL might looks something like:

.com/fr/UrlRewrites.html

And our Regular Expression might look something like:

^/fr\/(.*)\.html /index.php?friendly=$1

Now when our URL arrives at the server, it will get rewritten and sent to our sever-side processing like index.php?friendly=UrlRewrites and we can identify what dynamic content to display.

When using friendly names, you have to be careful to only use valid URL characters for your friendly name.  This means no spaces and no special characters.  The easiest way to accomplish this is to only allow word characters in your friendly name.  This is really simple to accompllish with a regular expression because you can just replace any non-word character(\W) with either an underscore or nothing.

In PHP this could be accomplished with something like:
$str = preg_replace('/\W/', '', $friendly_value);
Now all you have to do is check this value against the existing friendly names in your database to make sure it doesn't already exist.
SELECT count(DISTINCT(friendly)) FROM myMenus WHERE friendly like($str)
If it isn't already a friendly name, your good to go.