Go to the menu - Go to the contents

[Site map] You are here --- > Newbies Paradise > Tutorials > Unofficial > Website > Server > Reading a tutorial

URL Rewriting

Avatar
Author : Leo
Creation : : on 04/16/2008 05:06:28 PM
Last modification: : on 04/17/2008 04:50:55 PM
Rate and comment this tutorial
Print this tutorial
You are about to read a tutorial written by a member of this website. Although the author probably invested every effort to make this tutorial's content correct, we can not guarantee that the information on this page is 100% exact. Please keep this in mind when reading this tutorial. ;o)
Hi everyone ! My name is Leo. This course was originally published on the French version of this website, "Le Site du Zéro". It was written by Captain MDS. I got his authorization to translate it. If there's any mistake, you can send me a PM ;)

This is my first course about URL Rewriting.
What's that thing ?

This is a way to redirect your visitor. Ok, I'm sure you didn't understand. I'm going to give you an example : If you go to www.monsite.com/news-7-8.html, you'll be redirected on www.monsite.com/news.php?id=7&comm=8. Do you understand ? :)
It only redirects, but it doesn't replace the URL (it's not like NP, in fact ^^ ). To do this, it's not URL Rewriting but something more complicated. I don't know how to do it, so we're going to learn how to do URL Rewriting. :p

Be careful : the method I'm going to develop isn't available with all the hosts ! Anyway there are other ways to do it ; to find them, Google is your friend !

Let's go !
Chapter Contents :

What can I do with it ? How does it work ?

What can I do with it ?



Indeed, if I introduce it to you this way, I don't think you understand what you can do with it. :) It's very easy : Google and the other bots have difficulties to index the "exotic" URLs, such as this : news.php?id=8&comm=7. For instance, Google doesn't go further than 2 variables after the *.php. So if you've got many pages like that on your website, you're going to have difficulties with referencing them. URL Rewriting intervienes here.

How does it work ?



You can do it thanks to a .htaccess. You've got to put it at the root of your website. You've got to use the Apache mod_rewrite. But as I told you in the red message in the introduction, it's not enabled with every hosts.

Before we start

Before learning anything, let's check that the mod_rewrite is enabled.

If you've already got a .htaccess file in your website's root, add these lines, don't create another one ;)

Once both files are on your server, go to http://www.yourwebsite.com/try.html

There are 2 possibilities :


Now, let's start for good ! :pirate:

Coding

Theory



Eventually we can start ! :)
First, I'm going to explain to you the code we saw before.

Code: Apache
1
2
RewriteEngine on
RewriteRule ^try\.html$ /try.php [L]


The first line, RewriteEngine on, means that we enable the Apache mod_rewrite. It's the most important line, as without it, nothing will work. ^^

Then, the second line. RewriteRule means that we're going to define a redirecting rule (here, try.html becomes try.php). ^try\.html$ is a regular expression (a REGEX). It's not really difficult to understand : the URL must be written between ^ and $. Then, you just have to write the new URL. At the end of the line, [L] means that this rule is the last one to be applicated to this case, and that the module mustn't try to rewrite it.

You've got to put a \ before a . or it won't work.


At last, /try.php is the URL which have to be re-written. You had better put the absolute path for this page, as some hosts need it.

Here it is ! Ok, it won't be really useful for your website but I'm going to give you something much more useful ! This code will rewrite all your pages ending with *.php in pages ending with *.html.

Code: Apache
1
RewriteRule ^([0-9a-zA-Z-]+)\.html$ /$1.php [L]


Don't panic, I'm going to explain to you the changes. ([0-9a-zA-Z-]+) is a REGEX which means "any letter and as many times as you want". It must be put into brackets. On the other side, $1 is the name of your page *.php. It's going to be written again exactly the same way, but it will end with *.html. The brackets mean that you are modifying $1.

So, thanks to this RewriteRule, index.html redirects to index.php, liste-news.html redirects to liste-news.php, ...

And what about the "exotic" URLs ?

I was going to speak about it ! :)
It is the same thing, but with more variables ($1, $2). An example ? :p

Code: Apache
1
RewriteRule ^news-([0-9]+)-([0-9]+)-([0-9]+)\.html$  /news.php?idnews=$1&comm=$2&id=$3  [L]


Are you still with me ? :p

I'm going to explain, don't worry ^^
First, the URL we want to rewrite looks like to this /news.php?idnews=$1&comm=$2&id=$3 .
$1, $2 and $3 are the values for idnews, comm and id. So, instead of the URL you want to rewrite, you put this. On the other side, first you put the kind of URL you want (here news-$1-$2-$3.html but you can do somehow else if you want :p ) and then you replace $1, $2 and $3 by any number as many times as you want (REGEX).

Of course, you can put letters instead of numbers : in this case, do the same as we did before ( ([a-zA-Z0-9-]+) ).

If you want to gather the two examples I showed you, or to add others, remember that you first have to put the ones that are specific to a page, and then the others.


Practice



Have you understood everything ? Are you sure ?
Ok, prove it ! ^^ I'm going to give you some exercises for you to become the best in URL Rewriting. ;)

First exercise :

Write the rule that rewrites all your website's links which are like that : XXXX.php?var1=1&var2=3. They'll have to be like that then : XXXX-1-3.html. Easy, isn't it ? :p

Solution :

Spoiler (click to show)
Code: Apache
1
RewriteRule ^([a-zA-Z0-9-]+)-([0-9]+)-([0-9]+)\.html$ http://www.monsite.com/$1.php?var1=$2&var2=$3


Had you found it ? I don't have anything to explain : if you haven't understood, read again the theoric part. I've only gathered the two examples :)

Second exercise :

Write the rule that rewrites forum.php?page=showtopic&id=1558. It will have to be like that : forum/topic-1558.html.

Are you still alive ? ^^ I can't see any difficulty, can you ?

Spoiler (click to show)
Code: Apache
1
RewriteRule ^forum/topic-([0-9]+)\.html$ http://www.monsite.com/forum.php?page=showtopic&id=$1


Easier than it looks, doesn't it ? :)

Other uses of URL Rewriting

Ban a whole domain



Thanks to URL Rewriting, you can also ban the users who come from a precise domain, and then redirect them to another website.
Example :

RewriteEngine on
RewriteCond %(HTTP_REFERER) ^http://(www.)?the_domain_to_ban.com
RewriteRule .* http://www.osef.tk [L]

If your domain is the one that is written, you'll be redirected on the website written in the RewriteRule.

Prevent from other users to use my pictures from my website



This script is really useful : it prevents the other websites from using your pictures and using a part of your bandwidth. It's quite simple to understand.

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.yourwebsitecom/.*$ [NC]
RewriteRule .*\.(gif|png|jpe?g)$ - [F]

You compare the domain name where you see both your picture and your domain. If it's the same, you display the picture, else you give an error 403 (prohibition to access the page). [NC] means that this line doesn't respect the case sensitivity (IMAGE.jpg, image.jpg, ImAgE.JpG, ... all work). [F] gives an error 403 if the conditions are respected (if the domain name where you see the picture is different from yours).

Ban a browser's users



Or else, something funnier. You can ban the users who use a precise browser and redirect them on another website. An example which makes me laugh :devil: :

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} MSIE
RewriteRule .* http://www.mozilla.org [L]

MSIE = Internet Explorer ^^

Mean, isn't it ?

OK, this course is over. I hope you understood everything. If you need help, you can send me a PM. :)
Author : Leo
Rate and comment this tutorial
Print this tutorial

Change your template | Learn more about NP | Site map | Terms of use | Rules | RSS feed | XHTML 1.0 | CSS 2.0
Powered by Simple IT SARL: Contact us

There is nothing else to read, you have to go up now !

Do you want to be published here? Contact us.

Number of Newbies connected 4 Newbies Connected | SQL requests 7 Requests | Page loading delay 0.0392s (0.0264s)