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 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
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
}
?>
Simple and useful. Thanks!
ReplyDeleteshare knowledge that is useful, thank you
ReplyDeleteGreat post :-)
ReplyDeleteA 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!
ReplyDeleteswitch($_GET['key'])
ReplyDelete{
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)
ReplyDeleteIts really helpful.
ReplyDeletebut can u please explain this code as I have no idea about .htaccess file and this script.
Hi
ReplyDeletei 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';
ReplyDeletethe 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
ReplyDeleteCan u send mail what exactly you want with url examples to srinivas[at]inbox.com
how can I add third parameter?
ReplyDeleteI'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?
ReplyDeletewhy not like this:
ReplyDeleteif(isset($_GET['page'])){
$key = $_GET['page'];
} else{
$key = "home";
}
require($page .".php");
use .htaccess nice but css file problem
ReplyDeletehi , i want to rewrite my url www.mybazaar.pk/details/property/index.php?id=77
ReplyDeleteTO
www.mybazaar.pk/details/property/77/
what shold i write in my htaccess file ? and what more i have to change my in my site ?
this is giving the 500 internal server error on my localhost and i am using wamp server.
ReplyDeleteCan anyone help me how can i remove this error.
Thanks for the information,
ReplyDeleteand i want to make sure I understand with correct method or not, and I write some example here.
if in user.php got more than 1 variable is it like this to write in .htaccess?
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?friends=$2
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?likes=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?friends=$2&page=4
sorry if I'm wrongly understand and my bad english.
Thanks
maybe i know already
ReplyDeleteRewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
mean
^([a-zA-Z0-9_-]+) user=$1 will change only result include a-z A-Z 0-9
and
([0-9]+)$ page=$2 will change only result include 0-9
if user=abcd&page=2
mean will become
domain.com/abcd/2
is it?
it works thanx but if you add at the add an extra slash -> / to your site url http://flickr.com/username/2/ <- slash aaded, you can see the root that document..
ReplyDeleteis there some fix for that?
It should work
ReplyDeleteRewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
Nice article
ReplyDeletei have an url like below
http://www.bodyftn.com/article.php?cate=2&page=2
I want it to display like this
http://www.bodyftn.com/tips/page2
how can i do this? kndly help me..
Does it work in localhost or not ?
ReplyDeletethis site is very use full thank u boss
ReplyDeletei like this site and functionality
ReplyDeleteHey srinivas,
ReplyDeletehow we redirect like facebook username
www.facebook.com/profile.php?username=srinivas&v=info
to
www.facebook.com/srinivas?v=info
?? facebook username
www.facebook.com/profile.php?username=srinivas&v=info
to
www.facebook.com/srinivas?v=info
??
does it work in localhost? i'm using xampp and i'd already configured apache. thanks in advance.
ReplyDeleteHi, What is the best way to allow
ReplyDeleteRewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?user=$1
for more than one php file? at the moment i have
users.php?user=
and stations.php?station=
how can i make htacess differ between the 2?
Thanks,
Jamie.
RewriteEngine On
ReplyDeleteRewriteRule ^mydir/([^/]*)\.php$ /mydir/index.php?id=$1 [L]
I Got
"Internal Server Error"
Please I really need your help on htaccess. Below is my htaccess and it's not working
ReplyDeleteRewriteEngine on
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)/$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ user?user=$1
My url does work if I redirect to say
localhost/user?user=eniga
To
Localhost/user/eniga
Please how can I get this to work
[email protected]. Tnx
when i type ç,ö and other turkish words, i get server not found response, how can i solve this ?
ReplyDeletethank you for this tutorial
hey, u missed
ReplyDeleteRewriteBase
Hi i can't use your code tell me why?
ReplyDeleteHi i need to rewrite url which has two parameters i could not figure out how to rewrite i tried many forums but i didnt. can i ve solution.
ReplyDeletestill its not working check here is the link where i have used it
ReplyDeletehttp://espeeglobal.com/facilities.php
click on any image it will open an album than you will see the parameter query not the rewrite.
My code for htaccess is
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.espeeglobal.com
RewriteRule (.*) http://espeeglobal.com/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ facilities-album.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ facilities-album.php?id=$1
Hi its not loading css file when using
ReplyDeleteOptions +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
For those of you having problems with your css or js files loading after implementing url rewrites, be sure you're either using absolute paths (ie, /path/to/file.css as opposed to path/to/file.css), have added the rel attribute set to canonical (ie, link rel="canonical" href="yourdomain.com"), or added a base filepath (base href="yourdomain.com"). These fixes should be applied to the html page that includes the css or js.
ReplyDeleteThe problems you're having stem from the server looking for the files within a directory that doesn't actually exist, or in one that doesn't contain the desired file.
cat it be done on localhost
ReplyDeletei am using wamp on windows, i hv turn on mod rewrite but its not working on my local host please help me what should i do....
this is url
http://localhost/OFFICE_PROJECTS/project1/categories.php?cid=52
i want to rewrite as
http://localhost/OFFICE_PROJECTS/project1/categories/52
Hi Srinivas,
ReplyDeleteThanks for simple code for such complecated process. your example code is working but I want to use it bit differently. I'm passsing ID and then username of that id in database should be in URL like if I pass this URL
http://www.mysite.com?id=4
Then the URL should be this:
http://www.mysite.com/username
(username of id=4)
Please tell me how can I do that?
Its really a wonderful blog. I learned a lot about htaccess RewriteRule from here. Thank u all guys n gals.
ReplyDeleteHi. I use this script
ReplyDeleteRewriteEngine 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
but when it loads, css doesn't included. and i also trapped in that folder. how do i solve this? thanks
Hi
ReplyDeleteI need help How can i redirect multiple pages
Ex: 1.mysite.com/xxx.php to dfd
2.mysite.com/ddd.php?id=testst to mysite.com/testst
2.mysite.com/dffff.php?id=dfdfd to mysite.com/dfdfd
Hi Srinivas,
ReplyDeleteGood job man. I read your posts almost daily.
I have a doubt here,
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
Suppose the rewrite rule is written as above, the url will be rendered as
domain.com/name/2.
Now, will I be able to use $_GET global variable to fetch the 'name' and '2'?
If yes, could you please let me know how?
My htaccess files isn't working on my cpanel. What is the problem?
ReplyDeleteplz help me :(
ReplyDeletehttp://localhost/TM/testing.php?user=abcd
to
http://localhost/TM/testing.php/abcd
i am using this code in .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /TM/testing.php?user=$1
better use switch case than if else. switch case more faster than if else.
ReplyDeleteThank you so much Srinivas. This was exactly what I was looking for. Used it and works like a charm!!
ReplyDeleteNice URL Re-writing ...
ReplyDeletevishal you are right i have also doubt on multi parameter url.
ReplyDeleteHi
ReplyDeleteI need help How can i redirect multiple pages
Ex: 1.mysite.com/ex1.php to ex1
2.mysite.com/ex2.php?id=A3xd to mysite.com/ex2/A3xd
2.mysite.com/ex3.php?id=100 to mysite.com/ex3/100
thank you
Excellent post
ReplyDeleteIts really a wonderful blog.
ReplyDeleteHi its not loading css file when using
ReplyDeleteRewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ product.php?user=$1&page=$2
i want to Write Pretty URL.... RewriteCond method is need??
ReplyDeletehi i want my Url
ReplyDeletehttp://localhost:8080/public_html/sub-products?catid=42
to be writen like
http://localhost:8080/public_html/sarees
For my .htaccess i have used the following code
RewriteRule ^sarees$ :8080/public_html/sub-products?catid=$1 [L]
How can i do this?
is there any code i need to include in my php file too apart from .htacceess?
how didi you made subdomains in your website like
ReplyDeletedemos.9lessons.info
labs.9lessons.info
i can't get it?
Awesome !!! It work like charm ....
ReplyDeleteI use it to make pretty URL affiliate web script for my client
Hello,
ReplyDeleteI want my url
http://mydomain.com/article.php?url=Category/Subcategory/news-updates-today.html
to be written like this
http://mydomain.com/Category/Subcategory/news-updates-today.html
or
http://mydomain.com/Category/Subcategory/news-updates-today
Hi..
ReplyDeleteI want to redirect this url
from: http://www.example.com/company.php?company_name=sreelakshmi-traders&id=2
to: http://www.example.com/company/sreelakshmi-traders.html
Want to ignore id in url, please help me to do
Thanks
@Karthik
ReplyDeleteRead this http://www.9lessons.info/2011/04/seo-friendly-urls-with-php.html
current url:- http://localhost/naatcast/dashboard.php?key=sandeep
ReplyDeletehow to use http://www.example.com/username.
how to remove dashboard.php?key= and add username sandeep
http://localhost/naatcast/sandeep
RewriteEngine On
ReplyDeleteRewriteRule ^([a-zA-Z0-9_-]+)$ dashboard.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ dashboard.php?key=$1
its didn't work......
seo frndly url kaise karte hai...
ReplyDeleteSEO Friendly with .htaccess and PHP
Deletehttp://www.9lessons.info/2011/04/seo-friendly-urls-with-php.html
Thanks Tamada Sir For this great tutorial.
ReplyDelete