The mod_rewrite and the magic of rewriting the URL (second part)

In the first part of this article we’ve seen two simple examples of  url rewrite. What we will do now is to insert the name of the product in the url. Why?

Since the rewritten url will be the one registered from the search engine, it will be extremely interesting to visualize the product’s name in the url.

An aspect that we should pay great attention regards the limit  for which the string of  an url doesn’t accept  spaces and special or accented characters. So, we should pass the string that represents the name of the product through a filter that replaces the spaces with dashes, the accented letters with the same letters not accented and that eventually eliminates the special characters.

You can see an example of this technique in the url that contains this article. WordPress uses the method of the permalink, that transforms the title of the article in  an  univocal data. If you use  WordPress, you can try to create an article and entitle it  prova test.  A permlink  prova-test will be created.  Now if you try  to create another article and entitle it again prova test, the permalink prova-test-2 will be created. This is because the permlink will be the only data available to go and get the article and so- like I said-  has to be an univocal data

The system used by  WordPress  is very complicated, it  expects transformations and verifications and uses asynchronous callings. In this article we’ll see a  simplified model , but  valid  (anyway, I’m from the old school and as far as I’m concerned  the univocal data has one name: primary key ). Then we’ll  pass the name of the product, and somehow  also the id.

What kind of  url  we want to obtain to optimize the indexation?

The url that we want to obtain is of  this kind:


http://www.yoursite.com/producto56/big-electric-trimmers

If we wouldn’t use the url rewrite,  the construction of the link to visualize the details of the product would be like this:

//reading values from the database
echo '<a href="products.php?id=' . $id .  '">' . $nameProduct . '</a>';

Instead, if we want to produce an url  like the one previously seen,  we should go on this way:

//reading values from the database
//filtering product name
echo '<a href="product' . $id . '/' .  $nameFilteredProduct . '">' . $nameProduct . '</a>';

As we have seen in the beginning, if we want to pass the product’s name in the url, we should first clean it up of  accented letters and special characters that can give problems and afterwards replace also the spaces with dashes.

Clean up the product’s name that we want to pass as url

We create the function CleanString()  and start replacing the accented letters with the same letters without accents using the function  str_ireplace().

 function CleanString($string)
 {
     $strResult = str_ireplace("à", "a", $string);
     $strResult  = str_ireplace("á", "a", $strResult);
     $strResult =  str_ireplace("è", "e", $strResult);
     $strResult =  str_ireplace("é", "e", $strResult);
     $strResult =  str_ireplace("ì", "i", $strResult);
     $strResult =  str_ireplace("í", "i", $strResult);
     $strResult =  str_ireplace("ò", "o", $strResult);
     $strResult =  str_ireplace("ó", "o", $strResult);
     $strResult =  str_ireplace("ù", "u", $strResult);
     $strResult =  str_ireplace("ú", "u", $strResult);
     $strResult =  str_ireplace("ç", "c", $strResult);
     $strResult =  str_ireplace("ö", "o", $strResult);
     $strResult =  str_ireplace("û", "u", $strResult);
     $strResult =  str_ireplace("ê", "e", $strResult);
     $strResult =  str_ireplace("ü", "u", $strResult);
     $strResult =  str_ireplace("ë", "e", $strResult);
     $strResult =  str_ireplace("ä", "a", $strResult);

We also replace the apostrophe with a space:

$strResult =  str_ireplace("'", " ", $strResult);

Now we can remove everything that isn’t a normal character or number:

$strResult  = preg_replace('/[^A-Za-z0-9 ]/', "", $strResult);

Afterwards, we remove  spaces before  and/or after the string with the function  trim():

$strResult = trim($strResult);

Now  a final touch.  If there’s double (or more) spaces inside the string, we’ll reduce them to one only space:

$strResult  = preg_replace('/[ ]{2,}/', " ", $strResult);

The previous regular expression finds everything that has two or more spaces and replaces it with one single space.

Now  we have to replace the spaces with dashes:

$strResult = str_replace("  ", "-", $strResult);

And here’s our function:

function CleanString($string)
{
    $strResult = str_ireplace("à", "a", $string);
    $strResult  = str_ireplace("á", "a", $strResult);
    $strResult =  str_ireplace("è", "e", $strResult);
    $strResult =  str_ireplace("é", "e", $strResult);
    $strResult =  str_ireplace("ì", "i", $strResult);
    $strResult =  str_ireplace("í", "i", $strResult);
    $strResult =  str_ireplace("ò", "o", $strResult);
    $strResult =  str_ireplace("ó", "o", $strResult);
    $strResult =  str_ireplace("ù", "u", $strResult);
    $strResult =  str_ireplace("ú", "u", $strResult);
    $strResult =  str_ireplace("ç", "c", $strResult);
    $strResult =  str_ireplace("ö", "o", $strResult);
    $strResult =  str_ireplace("û", "u", $strResult);
    $strResult =  str_ireplace("ê", "e", $strResult);
    $strResult =  str_ireplace("ü", "u", $strResult);
    $strResult =  str_ireplace("ë", "e", $strResult);
    $strResult =  str_ireplace("ä", "a", $strResult);
    $strResult =  str_ireplace("'", " ", $strResult);

    $strResult = preg_replace('/[^A-Za-z0-9 ]/', "", $strResult);
    $strResult = trim($strResult);
    $strResult =  preg_replace('/[ ]{2,}/', " ", $strResult);

    $strResult = str_replace(" ", "-", $strResult);

    return $strResult;
}

In this way the creation of our link will be  very simple:

//reading values from the database
echo '<a href="product' . $id . '/' .  CleanString($nameProduct) . '">' . $nameProduct . '</a>';

Let’s write the rewrite url rule

At this point we can go on with the implementation of the rewrite rule in this way:

RewriteEngine On
RewriteRule  ^prodotto([0-9]+)/([a-zA-Z0-9-]+)$ prodotti.php?id=$1

This rule provides that: when you find the string product followed by a one or two digit number, followed by  “/”, followed by an alphanumeric string (that can contain the the character  “-”)  of one ore more characters, ask for the page products.php?id=theNumberfounInTheFirstSubstringilNumeroCheHaiTrovatoNellaPrimaSottostringa.

The second substring doesn’t matter because we already have the id, necessary to load the details of the product, but  we are interested in the optimization for the search engines, for which on the url level the name of the product is available.

Conclusions

We’ve seen three fundamental examples  of how to implement the rewriting of the url. Theoretically there are no limits to the possibilities offered  by this  Apache model, the only limit may be the knowledge we have of the regular expressions. In fact, as we have seen,  the major part deals with this. And you? Do you use this technique to improve the indexation of your sites?

Master per Web Designer Freelance
In tutti questi anni abbiamo ricevuto centinaia di richieste di approfondimento sulle numerose tematiche del web design vissuto da freelance. Le abbiamo affrontate volta per volta. Ma ci siamo resi conto che era necessario fare qualcosa di più. Ecco perché è nato One Year Together, un vero e proprio master per web designer freelance che apre finalmente le porte al mondo del lavoro.
Scopri One Year Together »
[pdf]Scarica articolo in PDF[/pdf]
Tags: , ,

The Author

Maurizio is married to the triad PHP - MySql - Apache and, not enough, he has a lover called jQuery. He has a blog where he tries to describe in detail all of "his lovers". His real specialty is the realization of large business application, altough he never refuses the commitment of a website.

Author's web site | Other articles written by

Related Posts

You may be interested in the following articles:

2 comments

Trackback e pingback

  1. Tweets that mention The mod_rewrite and the magic of rewriting the URL (second part) | Your Inspiration Web -- Topsy.com
    [...] This post was mentioned on Twitter by soshableweb and Web RSS News, Antonino Scarfì. Antonino Scarfì said: RT @YIW …

Leave a Reply

Current month ye@r day *