Sunday, June 15, 2008

URL Rewriting using PHP and Smarty

Let's learn to rewrite the website url today.

According to me, before learning something new, we should take a look at why we need to do it and what's the use of it.

Use of URL rewriting:
1. Making website URLs more user and search engine friendly.
2. Preventing undesired "inline linking".
3. Not exposing the inner workings of a web site's address to visitors

So let's begin...

Project Name: url_rewriting
Dir. Structure: url_rewriting
- cache
- configs
- libs
- templates
- templates_c
- .htaccess
- other php files

What we want to achieve:
here we want to convert the following url
from,
http://www.orkut.com/UniversalSearch.php?origin=box&exp=1&q=php
to
http://www.orkut.com/box/1/php/UniversalSearch.html

How we can achieve:
we need to first create a file "function.seo_optimise.php" in "smarty/lib/plugins/", so that smarty will treat it as its own function without any hassal.


function smarty_function_seo_optimise($params, &$smarty)
{
// we will add code here later...
}
?>

Done... 50%

Now secondly, we have to create a .htaccess file in the directory structure as shown above. And add following...

php_value error_reporting 7
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/([0-9]+)/(.*)/UniversalSearch.html$ UniversalSearch.php?origin=$1&exp=$2&q=$3

Please note that, (.*) suggest the type of text we are sending to perticular querystring. Like for origin querystring, we are going to send anything including characters & numbers, or in case of exp querystring, we are going to send only numbers thats why we have used ([0-9]+) in order to restrict the querystring value to the numbers only.

Done... 70%

I know, you are confused now, because you are thinking that how smarty will know, (.*) for origin, ([0-9]+) for exp and (.*) for q. Do not worry, thats we have created "function.seo_optimise.php" file above. Now, open this file, and write the following code... please note that here we are going to send three querystrings to the page thats why we have to pass three parameters to the function also. These are the three parameters..
function smarty_function_seo_optimise($params, &$smarty)
{
/* This is for three querystrings */
$origin = $params[origin];
$exp = $params[exp];
$q = $params[q];
/* This is to recognise the page to which we are going to rewrite */
if($params[type] == 'universal_page')
{
if($params[urls] == 'Enable')
return $origin.$exp.$q."/UniversalSearch.html";
else
return "UniversalSearch.php?origin=".$origin."&exp=".$exp."&q=".$q
}

}

Done... 80%

Now let me explain you, this smarty function will transform our old url to new like this... http://www.orkut.com/box/1/php/UniversalSearch.html Ok then, time to spell the beans... to convert that url we have to write following in .tpl file.

{seo_optimise urls=$seo_urls origin=box exp=1 q=php type=universal_page}

<a href="http://www.blogger.com/%7Bseo_optimise%20urls=$seo_urls%20origin=box%20exp=1%20q=php%20type=universal_page%7D">
Click me
</a>


here is a explaination...

$seo_urls => Enable/Disable


when the page get loaded, that seo_optimise will pass the supplied parameters to the function of a same name created in smarty/lib/plugins/function.seo_optimise.php.

1. First, it will add the passed value to appropriete variables as mentioned above in this php file.

2. secondly, it will check which page you want to rewrite using "type" variable.
3. If it is "universal_page" and $seo_urls is "Enable" then it will return the url like http://www.orkut.com/box/1/php/UniversalSearch.html or if $seo_urls is "Disable" then it will return the url like http://www.orkut.com/UniversalSearch.php?origin=box&exp=1&q=php
Done... 100%

Using this way we can rewrite any url in any ways you want, just do not forget to enter the old url & new url in .htaccess file.