Pretty URLs with .htaccess Rewriting Tips.
Wall Script
Wall Script
Monday, November 23, 2009

Pretty URLs with .htaccess Rewriting Tips.

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
}
?>
web notification

64 comments:

  1. share knowledge that is useful, thank you

    ReplyDelete
  2. 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/"

    ReplyDelete
  3. 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.

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

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

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

    ReplyDelete
  7. 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

    ReplyDelete
  8. @keno

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

    ReplyDelete
  9. how can I add third parameter?

    ReplyDelete
  10. 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?

    ReplyDelete
  11. why not like this:


    if(isset($_GET['page'])){
    $key = $_GET['page'];
    } else{
    $key = "home";
    }

    require($page .".php");

    ReplyDelete
  12. use .htaccess nice but css file problem

    ReplyDelete
  13. hi , i want to rewrite my url www.mybazaar.pk/details/property/index.php?id=77
    TO
    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 ?

    ReplyDelete
  14. this is giving the 500 internal server error on my localhost and i am using wamp server.
    Can anyone help me how can i remove this error.

    ReplyDelete
  15. Thanks for the information,
    and 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

    ReplyDelete
  16. maybe i know already
    RewriteEngine 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?

    ReplyDelete
  17. 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..

    is there some fix for that?

    ReplyDelete
  18. It should work

    RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2

    ReplyDelete
  19. Nice article
    i 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..

    ReplyDelete
  20. Does it work in localhost or not ?

    ReplyDelete
  21. this site is very use full thank u boss

    ReplyDelete
  22. i like this site and functionality

    ReplyDelete
  23. Hey srinivas,
    how 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

    ??

    ReplyDelete
  24. does it work in localhost? i'm using xampp and i'd already configured apache. thanks in advance.

    ReplyDelete
  25. Hi, What is the best way to allow

    RewriteEngine 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.

    ReplyDelete
  26. RewriteEngine On
    RewriteRule ^mydir/([^/]*)\.php$ /mydir/index.php?id=$1 [L]

    I Got

    "Internal Server Error"

    ReplyDelete
  27. Please I really need your help on htaccess. Below is my htaccess and it's not working

    RewriteEngine 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

    ReplyDelete
  28. when i type ç,ö and other turkish words, i get server not found response, how can i solve this ?

    thank you for this tutorial

    ReplyDelete
  29. Hi i can't use your code tell me why?

    ReplyDelete
  30. Hi 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.

    ReplyDelete
  31. still its not working check here is the link where i have used it
    http://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

    ReplyDelete
  32. Hi its not loading css file when using

    Options +SymLinksIfOwnerMatch
    RewriteEngine on

    RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2

    ReplyDelete
  33. 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.
    The 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.

    ReplyDelete
  34. cat it be done on localhost
    i 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

    ReplyDelete
  35. Hi Srinivas,

    Thanks 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?

    ReplyDelete
  36. Its really a wonderful blog. I learned a lot about htaccess RewriteRule from here. Thank u all guys n gals.

    ReplyDelete
  37. Hi. I use this script

    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

    but when it loads, css doesn't included. and i also trapped in that folder. how do i solve this? thanks

    ReplyDelete
  38. Hi

    I 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

    ReplyDelete
  39. Hi Srinivas,

    Good 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?

    ReplyDelete
  40. My htaccess files isn't working on my cpanel. What is the problem?

    ReplyDelete
  41. plz help me :(
    http://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

    ReplyDelete
  42. better use switch case than if else. switch case more faster than if else.

    ReplyDelete
  43. Thank you so much Srinivas. This was exactly what I was looking for. Used it and works like a charm!!

    ReplyDelete
  44. vishal you are right i have also doubt on multi parameter url.

    ReplyDelete
  45. Hi

    I 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

    ReplyDelete
  46. Hi its not loading css file when using

    RewriteEngine on

    RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ product.php?user=$1&page=$2

    ReplyDelete
  47. i want to Write Pretty URL.... RewriteCond method is need??

    ReplyDelete
  48. hi i want my Url
    http://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?

    ReplyDelete
  49. how didi you made subdomains in your website like
    demos.9lessons.info
    labs.9lessons.info
    i can't get it?

    ReplyDelete
  50. Awesome !!! It work like charm ....
    I use it to make pretty URL affiliate web script for my client

    ReplyDelete
  51. Hello,

    I 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

    ReplyDelete
  52. Hi..

    I 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

    ReplyDelete
  53. @Karthik

    Read this http://www.9lessons.info/2011/04/seo-friendly-urls-with-php.html

    ReplyDelete
  54. current url:- http://localhost/naatcast/dashboard.php?key=sandeep

    how to use http://www.example.com/username.
    how to remove dashboard.php?key= and add username sandeep
    http://localhost/naatcast/sandeep

    ReplyDelete
  55. RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ dashboard.php?key=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ dashboard.php?key=$1


    its didn't work......

    ReplyDelete
  56. seo frndly url kaise karte hai...

    ReplyDelete
  57. Thanks Tamada Sir For this great tutorial.

    ReplyDelete

mailxengine Youtueb channel
Make in India
X