Pretty URLs always helps you to boost up search engine page ranking. In this post I want to explain about URLs rewriting techniques using .htaccess file. Friendly URLs looks neat on the browser address bar. Take a look at live demo

Download Script
Live DemoLong days back I had discussed about .htaccess basic rewriting tips Eg. First parameter Domain.com/users?id=9lessons to Domain.com/9lessons take a look the following link.
This time I'm talking about rewriting second parameter URLs using .htaccess file.

Original URL.
Eg. http://flickr.com/users.php?id=username&page=2
Rewriting Friendly URL.
Eg. http://flickr.com/username/2
.htaccess Code
//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?user=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?user=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
Twitter like Pretty URLs

.htaccess Code
Just rewriting first parameter.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1
index.php
Contains simple PHP code.
<?php
$key=$_GET['key'];
if($key=='home')
{
include('home.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>
$key=$_GET['key'];
if($key=='home')
{
include('home.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>










![srinivas.tamada[at]gmail.com](http://lh4.ggpht.com/_N9kpbq3FL74/SgknVlmcrAI/AAAAAAAABns/OhTsS0WO_Sw/gtalk.png)






Simple and useful. Thanks!
share knowledge that is useful, thank you
i see thanx you vru nice post alışveriş
Great post :-)
A great intro to using mod_rewrite... One thing I do a little differently is to use the "?" character like so:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ users.php?user=$1
That way the regex will match both "something" and "something/"
Very nice!
switch($_GET['key'])
{
case 'home': include('home.php');break;
case 'login': include('login.php');break;
case 'terms': include('terms.php');break;
case 'users':include('users.php');breaak;
default: include('404.php');
}
Much better.
Agree with the previous poster. (I was going to say the same thing)
Thanks for the info
Its really helpful.
but can u please explain this code as I have no idea about .htaccess file and this script.
Hi
i want a script for preventing XSS attacks in PHP.
can u please help me...
it will be so nice of u..
thnx
hi Srinivas, great works man. i need a little help, i'm trying to rewrite a url with two variables.. view.php?id=1&title='some text';
the problem is, the 'id' queries my database but in the final result i only want the 'title' variable displayed along the url without the 'id' showing. how do i write the .htaccess code. Thanks
@keno
Can u send mail what exactly you want with url examples to srinivas[at]inbox.com
how can I add third parameter?
I've got a problem, when I use 2 or 3 parameters, I've got worries with the relative links of my css, my js.. because they refers to the numbers of / in the url and they think they are in a subfolder.. Any ideas?