Friendly URL! is it really needed?

The trend of the web right now is to avoid as much as possible to pass GET’s variables to a web site, instead of that it is used “friendly URL”.

This help to increase visitors to your web site, because the URL describe the content of your web page, and web search engine shows your page result on the firsts position, this is on one important point on SEO, this is because “ajax-ajax-and-more-ajax.html” its more descriptive than “foo.php?id=6″

You may be wondering is there is a real file called “ajax-ajax-and-more-ajax.html”?, the answer is not, most of web-server has a Rewrite Engine. A Rewrite Engine is a web server module that helps to translate URL into a real file. A little trick to give fake names to pages, for many reasons, the most important are, hide the real name of the script, SEO (Search Engine Optimization) the URL could describe the content of the page, preventing undesired “inline linking” or “hot linking”.

The big problem is that you must know how regular expression works. Also, another important problem is that every web-server has a difference method to define URL rewrites.

For handle those needs I’ve wrote the PHP Class URL Rewriter it could handle any rewriter engine and include the right information. In the example only Apache mod_rewrite is wrote because I don’t have access to others servers.

How to start with URL Rewriter

URL Rewriter comes with a set of regular expressions, it is not PHP expressions, basically you have variables “[]”, optionality “{}” and you can force that a variable be in a set of posibilities

require “url_rewriter.php”;
$url = new url_rewriter(‘example-urlrewriter/’);
$country=array(‘Argentina’,‘Brazil’,‘Paraguay’);

$url->add_rule(
“page.php”
,
“/[country]/
[type]/page{-index.html}”,
array(
‘country’=> $country) );

This will match if [country] is Paraguay, Argentina or Brazil, [type] could be anything, and the third token must be index.php or nothing. The following examples will match:

/Paraguay/foo/page-index.html
/Brazil/bar/page

If URL match with the regular expression “page.php” is include, and “page.php” receives a variable “$urlVars”, which is an array with all the parameters (in the first example would be $urlVars[’country’]=”Paraguay” and $urlVars[’type’]=”Foo”).

Actually the optionality is not recursive, so you cannot define {index-{.html}}. There is also an special variable [:numberX] where X could be anything, only match if the token is a number.

With URL Rewriter is easy to manage friendly URL (for example it could be loaded from a database) and the parsing of the URL.

Also URL Rewriter is full compatible with Auto Ajax, with the mix with this two projects you could write Ajax sites’ with friendly url.

Download PHP URL Rewriter and Auto Ajax.

5 Comments

  1. Comment by julien on January 3, 2008 9:49 am

    Nice class! I wish to give it a try soon.

  2. Comment by Reza on January 6, 2008 7:51 am

    Great solution!

  3. Comment by Linda on June 27, 2008 11:58 am

    a good Tip for SEO optimization is to have unique content! The only problem is that it is hard to get content sometimes. So I have come up with a solution for that. An Automated Article Re-writter. It will ranomly change almost every word in your article. One you have done this you can submit this article to your website/blog and google will think its unique content!! This rewritter is free too! http://randompaper.com

  4. Comment by Mulkazarus on March 13, 2009 6:36 pm

    2]Get your own Nintendo Wii
    The games console with one of the strangest names on the planet (it’s pronounced ‘wee’) is also one of the most innovative. In fact, with its potential to change the face of the gaming landscape, Wii may be on the verge of a new era, if you’ll pardon the pun.
    The $400 Wii package comprises a square white console unit and stand that plugs into your TV or AV receiver, a wireless sensor bar that connects to the console and receives wireless commands from the battery-powered paddle-style Wiimote controller. Basic composite cables are supplied, but if you have a plasma or LCD the $50 component cable options will deliver better picture quality. The console also supports an SD slot, USB port and a DVD drive for games.
    Games data can be saved to SD memory cards, which weren’t officially released at the time of publication, but in lieu of their arrival, GameCube memory cards will suffice. The USB port can’t be used to save games either, but will enable future hardware upgrades such as a hard drive or DVD player, although no announcements have been made to this effect.
    Despite its DVD drive, the console is not a DVD player (a modification chip is required if you want to watch movies). Nor is it the high definition, hard drive-toting, networked multimedia online multiplayer gaming machine that is the Xbox 360 or forthcoming Playstation 3. Instead, it presents as the most affordable ‘next generation’ games machine available and, certainly, lacks for no important ingredients if you want to have fun.

  5. Comment by sandrar on September 10, 2009 7:36 pm

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

Comments RSS

Leave a comment