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)
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 learning anything, let's check that the
mod_rewrite is enabled.
- If you've got a private server, open this file : httpd.conf. It's in the Apache directory. Then, check that these two lines aren't commentaries (a line which starts with a # is a commentary) :
Code: Apache1
2 | LoadModule rewrite_module modules/mod_rewrite.so
AddModule mod_rewrite.c
|
If they are, just remove the #
- Else, we're going to check whether it's enabled or not. To do it, create a new file try.php and put it at the root of your website. Put anything in it. Then, create a .htaccess file (htaccess.txt and you'll rename it once it's on the server, as Windows doesn't like files starting with a .) and put these 2 lines in it. I'll explain them to you then :
Code: Apache1
2 | RewriteEngine on
RewriteRule ^essai\.html$ /essai.php [L]
|
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 :
- either you see the page try.php. It means that the mod_rewrite is enabled, so we can continue
- or you see an error. It means that the mod_rewrite is disabled. Unfortunately, you can't continue this tutorial. Now remove the .htaccess file from your server.
Now, let's start for good !
Theory
Eventually we can start !

First, I'm going to explain to you the code we saw before.
Code: Apache1
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: Apache1 | 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 ?
Code: Apache1 | RewriteRule ^news-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /news.php?idnews=$1&comm=$2&id=$3 [L]
|
Are you still with me ?
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

) 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 ?
Solution :
Spoiler (click to show)Code: Apache1 | 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: Apache1 | RewriteRule ^forum/topic-([0-9]+)\.html$ http://www.monsite.com/forum.php?page=showtopic&id=$1
|
Easier than it looks, doesn't it ?
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

:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} MSIE
RewriteRule .*
http://www.mozilla.org [L]
MSIE = Internet Explorer
Mean, isn't it ?