Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do you want to implement your own search friendly url engine?
#1
This is one version of URL rewrite engine, which is not dependent on Apache mod_rewrite. It is relatively easy to implement in other systems or to adapt to your specs. We currently use this version on some of our

This is an evil url: http://www.notfriendly.com/index.php?men...pe=article
This is a good url: http://www.friendly.com/index/menuID_28-type_article

My solution is to the URL processing in PHP. If you follow these steps you will have your own Apache compatible URL rewrite engine in no time.

Step 1.

In your .htaccess file (If you don't have one, then create a new one in the folder where your index.php resides) add the following:

<FilesMatch "index">
ForceType application/x-httpd-php
</FilesMatch>

This simply tells your apache server to treat "index" in the url as "index.php". If you are using a server other than apache, then you will need to configure it to do this.

Step 2.

Implement the class below so that an instance is create each time index.php is executed. What this does is it takes the special url http://www.friendly.com/index/menuID_28-type_article and splits it into http://www.friendly.com/index/ and menuID_28-type_article strings, then it creates an array:

array['menuID'] = 28
array['type'] = article

You cannot use $_GET['paramName'], instead call getItem($key) method in UrlFilter passing the name of the parameter you want, for exaple: $this->urlFilter -> getItem("menuID").

You can use any number of parameters, and name them as you wish, all you have to do is use the getItem() method and you got yourself nice URLs. Ofcourse you will need to build your links correctly for this to work, use getOriginalUrl() for this, then add your parameters to it: /param_value-param2_value2-param3_value3 and so on...

// constructs search friendly urls and create correct url paths for links and images

class UrlFilter {
var $origUrl;
var $numOfItems;
var $itemsMap;

function UrlFilter()
{
$this->processURI();
$this->constructBaseUrl();
}

function processURI()
{
$array = explode("index", $_SERVER['PHP_SELF']);
$newUrl = str_replace("/", "", $array[count($array)-1]);
$array = explode("-", $newUrl);
$num = count($array);
$this->numOfItems = $num;
$url_array = array();

$items = explode("-", $newUrl);
$num = count($items);
for ($i = 0 ; $i < $num ; $i++) {
$item = explode("_", $items[$i]);
$this->itemsMap[$item[0]] = $item[1] ;
}
}

function constructBaseUrl()
{
// need to construct the original url
$array = explode("index", $_SERVER['PHP_SELF']);
$num = count($array);
$this->origUrl = "";
for ($i = 0; $i < $num-1 ; $i++) {
$this->origUrl = $this->origUrl . $array[$i];
}
}

function getItem($key)
{
if (array_key_exists($key, $this->itemsMap)) {
return $this->itemsMap[$key];
} else {
return null;
}
}

function getOriginalUrl()
{
return $this->origUrl . "index";
}

function getBaseUrl()
{
return $this->origUrl;
}

}
Support
Virtual Web Designs
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)