The mod_rewrite and the magic of rewriting the URL (first part)
In this article, we’ll see the basics of a wide spread and useful technique, so called l’url rewrite. It deals with one of the Apache’s functionality made possible with the module mod_rewrite, implementable and configurable through our old friend: the files: .htaccess. The major benefit in rewriting the url is a better indexation from the search engines. It seems in fact that the spider are disturbed by the query string (?id=34&type=5&mod=user), particularly if they pass many parameters.
Are you ready to enter in this mysterious and interesting field of the web?
How can we translate the url ?
Let’s consider that in our site, the page products.php shows a product in base of the id passed through the url. .
This page shows the details of the product saved in the database with id=79.
with a simple rule, we can rewrite the url in this way:
All we have to do is say to Apache:
when you find in the url product followed by a dash followed by a number followed by .html, ask for the product page.php?id=thatNumber
As you have hinted, we need to work with the regular expressions. If you are new at this, don’t worry. Giustino wrote an excellent guide concerning this matter.
How to declare a rule?
The general model to declare a rewrite rule is the following:
RewriteEngine On RewriteRule what-i-think-to-find-in-url-bar how-i-have-to-translate-what-i-find
First of all, the motor of the mod rewrite gets initialized with the statement RewriteEngine On.
Afterwards, the rule (or the rules) get declared.
But let’s pass to the practice and implement what said in the previous paragraph.
We begin with writing the page products.php that we will simply create like this:
<?php echo 'This page display the product number ' . $_GET['id']; ?>
This will be enough to verify the correct functioning of our rule.
Now let’s see the file .htaccess that has to be saved in the same (or superior level) folder of the file products.php.
RewriteEngine On RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1
All that this rule says is exactly want we have seen previously: When you find in the url something that begins with (^ means beginning of the string) product followed by a dash followed by one or more digit number (+ means one or more) and ends ($ means the end of the string) with .html, ask for the page products.php?id=thatNumber
$1 refers to what has been found from the first (in this case only one) substring.
Now setting as url:
You should visualize on the video the phrase:
“This page shows the product number 34.”
An example with two parameters
Now if we suppose that our page products.php depends on two parameters: the id and the category of the product. The original URL could be this:
While rewritten may have this aspect:
So, let’s see a possible rule and then comment it:
RewriteEngine On RewriteRule ^([a-z]+)/product-([0-9]+)\.html$ products.php?category=$1&id=$2
What does this rule say?
If you find a string that begins with one or more characters followed by / followed by product followed by a dash followed by one or more digit number followed by .html, ask for the page products.php?category=theOneThatYouFoundInTheFirstSubstring=theOnethatYouFoundInTheSecondSubstring
We modify the file products.php so that we can verify also the functioning of this second example in this way:
<?php echo 'This page display the product number ' . $_GET['id'] . ' of the category ' . $_GET['category']; ?>
Now typing the url:
You’ll visualize on the video the following phrase:
“This page shows the product number 12 belonging to the beverage category.”
In this article we’ve seen the bases of the url rewrite. You realized how important it is a good knowledge of the regular expressions in order to master this technique. In the next article, we’ll see a very practical application in the SEO field, that is a rewrite similar to the one that implements wordpress with the permalink .
And you, do you have some url rewrite samples to show?
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 »