Loading Searchbox
9lessons programming blog logo
Monday, November 23, 2009

Pretty URLs with .htaccess Rewriting Tips.

14 comments
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

Pretty URLs with .htaccess Rewriting Tips.

Download Script     Live Demo

Long 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



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

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
}
?>
Sponsored Links

Recent Posts

Share this post

Subscribe to my feeds

Subscribe
Comments
14 comments
Jônatan Froes said...

Simple and useful. Thanks!

Anonymous said...

share knowledge that is useful, thank you

yoxul said...

i see thanx you vru nice post alışveriş

Chris Williams said...

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/"

Vishnu said...

Very nice!

Anonymous said...

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.

Anonymous said...

Agree with the previous poster. (I was going to say the same thing)

Rajesh Kanuri said...

Thanks for the info

Arvind.b said...

Its really helpful.
but can u please explain this code as I have no idea about .htaccess file and this script.

Arvind.b said...

Hi
i want a script for preventing XSS attacks in PHP.
can u please help me...
it will be so nice of u..
thnx

keno said...

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

Srinivas Tamada said...

@keno

Can u send mail what exactly you want with url examples to srinivas[at]inbox.com

Anonymous said...

how can I add third parameter?

Anonymous said...

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?

Post a Comment

Orkut | FacebookAbout Me

Subscribe now!Feeds RSS

Subscribe now!Recent Posts

Subscribe now!Categories

Subscribe now!Comments

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Join into my community

Labs ProfileRelease

My ProfileTwitter