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/pageIf 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.
3 Comments
Leave a comment







Nice class! I wish to give it a try soon.
Great solution!
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