5 useful url rewriting examples using .htaccess

Advertisement

If you are looking for the examples of URL rewriting then this post might be useful for you. In this post, I’ve given five useful examples of URL rewriting using .htacess. If you don’t know something about url rewriting then please check my older post about url rewriting using .htaccess.

Now let’s look at the examples

1)Rewriting product.php?id=12 to product-12.html

It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.

RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

2) Rewriting product.php?id=12 to product/ipod-nano/12.html

SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.

RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2

3) Redirecting non www URL to www URL

If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]

4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz

Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

5) Redirecting the domain to a new subfolder of inside public_html.

Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1

Popularity: 80% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» How to check and enable mod_rewrite module in apache
» How to redirect browser to https (ssl) in php
» Custom Error Page with .htaccess
» Hide .php extension with url rewriting using .htaccess

126 Comments on “5 useful url rewriting examples using .htaccess”

  • John Tittle wrote on 12 March, 2008, 3:55

    I’m thinking about optimizing my url query strings, but want to make sure there isn’t a basic problem with the structure of my site first.

    I recently changed my website from www-dot-dofsbo-dot-com to searches.tittlerealestate.com. The content remains very similar. Suddenly its as if the content of my site is no longer getting crawled. Hits from keywords are limmited to phrases on my home page. The tittlerealestate domain has been around for a long time and should be getting crawled.
    Could the “searches” portion of my url be causing the problem? What do you call the portion of a url preceeding the main url?

    Thanks for the article

  • Roshan wrote on 13 March, 2008, 11:22

    well you should have done 301 redirect to the new transferred domain then deleted the content from the old site. I think there is problem with the same content in both place i.e the massive duplicate content problem

  • Max wrote on 25 March, 2008, 21:54

    #2 – The title says the opposite of what its actually doing.

    #3 – I’m nitpicking but
    ^optimaxwebsolutions\.com
    will also match optimaxwebsolutions.com.otherwebsite.com

    #5 – Maybe I’m wrong here but I believe rewrite conditions (RewriteCond) must all be true for the RewriteRule to take place. So these two lines
    RewriteCond %{HTTP_HOST} ^test\.com$
    RewriteCond %{HTTP_HOST} ^www\.test\.com$
    can’t both be true, so this rule will never do anything. I think you want to use the [OR] flag.

  • Luke L wrote on 25 March, 2008, 22:31

    In the second example you put the value in pid, not id, that might catch newbies out.

    Also, in the fourth example you don’t check for existing directories, so if someone were to go to yoursite.com/about they would get the userpage for About.

  • Roshan wrote on 26 March, 2008, 4:55

    Luke, thanks for the heads up..i’ve corrected that one…and in the fourth example you should have rewrite condition if you’ve the about about up page in that manner..

    Max Thanks for the comment, I think i’m valid at point #2. And the other points are you made are absolutely right and i’ve corrected them. Thank for noticing that……

  • Jason wrote on 26 March, 2008, 5:31

    Hm.. having meaningful and clean urls could be done originally with something like Django. Where instead of the page being

    product.php?id=12

    You could actually just have..

    /products/12

    or /whatever/ you want by modifying the URLS file in the app’s directory.

    A much cleaner and more elegant solution.

  • Roshan wrote on 26 March, 2008, 6:12

    hi jason, I agree with you elegant solution but it doesn’t show the keyword in the URL and from seo point of view it’s better to put the keyword in the URL of the post.

  • cherouvim wrote on 26 March, 2008, 8:43

    Does [2] mean that the following urls will all point to the same resource?

    product/ipod-nano/12.html
    product/foobar/12.html
    product/test/12.html
    product/example/12.html

  • Roshan wrote on 26 March, 2008, 9:00

    ya cheruvim..it all point to the same page because according to the rewrite rule it will all call to the same page product.php?id=12

  • Boyan Kostadinov wrote on 26 March, 2008, 12:40

    Decent examples, thanks.

    However, I am a bit confused with the example vs. the caption. Such as:
    1) Rewriting product.php?id=12 to product-12.html
    but your rule is rewriting in the opposite way such as product-12.html to product.php?id=12

    Same thing applied for #2 and #4.

    The syntax for the rewrite rules is:
    RewriteRule Pattern Substitution (see http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule)

    I am missing something?

  • Roshan wrote on 27 March, 2008, 10:35

    dear Boyan Kostadinov, well don’t be confused … as you know the normal way of displaying url is product.php?id=12 and since we’re rewriting product.php?id=12 to something else the caption is like so..hope this helps..

  • lord55 wrote on 7 May, 2008, 18:35

    hi!!!
    i have enable rewrite.my htaccess start with:

    # ugly ugly hack to detect missing mod_rewrite
    # RedirectMatch must be to an absolute destination, so forces 500 error…
    ErrorDocument 500 “Elgg error: Apache does not have mod_rewrite loaded. Please check your apache setup.”
    RedirectMatch 302 .* index.php

    order allow,deny
    deny from all

    # Don’t listing directory
    Options -Indexes

    # Follow symbolic links
    Options +FollowSymLinks

    # Default handler
    DirectoryIndex index.php

    # php 4, apache 1.x

    # default memory limit to 32Mb
    php_value memory_limit 32M
    # to make sure register global is off
    php_value register_globals 0
    # max post size to 8Mb
    php_value post_max_size 8388608
    # upload size limit to 5Mb
    php_value upload_max_filesize 5242880
    # hide errors, enable only if debug enabled
    php_value display_errors 0

    # php 4, apache 2

    # default memory limit to 32Mb
    php_value memory_limit 32M
    # to make sure register global is off
    php_value register_globals 0
    # max post size to 8Mb
    php_value post_max_size 8388608
    # upload size limit to 5Mb
    php_value upload_max_filesize 5242880
    # hide errors, enable only if debug enabled
    php_value display_errors 0

    # php 5, apache 1 and 2

    # default memory limit to 32Mb
    php_value memory_limit 32M
    # to make sure register global is off
    php_value register_globals 0
    # max post size to 8Mb
    php_value post_max_size 8388608
    # upload size limit to 5Mb
    php_value upload_max_filesize 5242880
    # hide errors, enable only if debug enabled
    php_value display_errors 0

    RewriteEngine on

    i call a javascript from a header.php in the root inside “folder1″. i.e:

    -root
    htacess
    main.php
    header.php
    –folder1

    the script is:

    but the javascript doesn’t work and in firebug i have an error:
    syntax error
    [Break on this error] <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.or…

    …and i see in firebug an index page.

    can you give me an help to set this rule nad teach me why?!?! i try and try but no result!
    thanks in advance!!!

  • lord55 wrote on 7 May, 2008, 18:39

    the javascript i call with:
    script type=’text/javascript’ src=’folder1/?project=time’></script

    from header.php

  • tevan wrote on 14 May, 2008, 14:46

    thanks for this, roshan! I just used it at the last minute before launching a new site today.

  • Roshan wrote on 14 May, 2008, 14:56

    nice to hear that tevan…

  • danny wrote on 21 May, 2008, 20:00

    Dude!!! I spent 3 hours total yesterday messing w/ mod_rewrite and tutorials from other sites and I could figure out #4. Now that I know, life is tons easier!!!

    p.s. thats why I donated, bc of your great help

  • Roshan wrote on 22 May, 2008, 5:45

    wow danny nice to hear that…..you’re the first person to donate in this blog ….

  • Kenai wrote on 27 May, 2008, 23:41

    Man… I found this site today via google, and I’m bookmarkin’ it! I’m a very, very new PHP/mySQL dev, so I will be using this site tons! When I get some funds from commercial work, I will be donating. Thank you very much!

  • Roshan wrote on 28 May, 2008, 15:02

    nice to hear that Kenai..

  • Sunil wrote on 16 June, 2008, 6:38

    Hello.

    I want to rewrite my url which has two parameter. But I am having some problem in rewriting the url.

    I need to rewrite the url product.php?id=12&action=detail to product-12-detail.html.

    So please can u provide me the code???

    Waiting for reply..

    Byee and have a nice day.

  • Dru wrote on 26 June, 2008, 2:15

    test – this does not seem to work for me.

  • Dru wrote on 26 June, 2008, 2:51

    The question is: if a user goes to: http://www.someisp.com/ they actually are getting served from http://www.someisp.com/store/ yet they only see http://www.someisp.com/

  • DotNetGuts wrote on 24 July, 2008, 20:53

    Check out the simplest way of writing URL Rewriting

    http://dotnetguts.blogspot.com/2008/07/url-rewriting-with-urlrewriternet.html

  • Arvind wrote on 29 July, 2008, 9:06

    How to rewrite site.com to https://site.com ???

  • Roshan wrote on 29 July, 2008, 11:45

    @Arvind – you can check this post for detail about redirecting web site to https :

    http://roshanbh.com.np/2008/05/redirect-browser-https-ssl-php.html

  • Kelvin wrote on 21 August, 2008, 12:34

    I tried that on my apache setup. Doesn’t seem to work. I just don’t understand. Here’s my htaccess file:

    RewriteEngine on
    RewriteRule ^old\.html$ index.html

    Accessing old.html doesn’t bring me to index.html.

  • Kelvin wrote on 26 August, 2008, 8:20

    I had found the answer!
    I need to set the overrride in the http.conf to all!
    Still, thanks for the beautiful article.

  • Urban wrote on 16 September, 2008, 12:05

    I use this 5 usefull things thank you for tutorial :)

  • Usman wrote on 19 September, 2008, 5:04

    Thanks, that worked for me. Although there are other simpler rules for .htaccess redirection, but they don’t hide the folder name in address bar.
    This one hides the folder name in the address bar.

  • mike wrote on 25 September, 2008, 12:50

    A question, with .htaccess I can type in the URL
    http://www.mysite.com/index22 and get actually http://www.mysite.com/index.php?var=22 however if I have a link in this page linking to http://www.mysite.com/index.php?var=22 and click it the browser will show http://www.mysite.com/index22 or not? Because I have already constracted the site with many links linking to actual files and I do not want to change them all. I want went the user types http://www.mysite.com/index.php?var=22 the address in the URL to become http://www.mysite.com/index22 is it possible?

  • vick wrote on 26 September, 2008, 21:23

    hi, can anyone help me out
    i was able to rewrite urls for simple stuff e.g. to products.php?id=xxx but cant for the following:
    /test/ -> /test/index.php?page=xxx&op=xxx&id=xxx
    op and id are optional
    tried the following:
    RewriteRule /(a-z+)/ /index.php?page=$1
    RewriteRule /(a-z+)/(a-z+)/ /index.php?page=$1&op=$2
    RewriteRule /(a-z+)/(a-z+)/(0-9+)/ /index.php?page=$1&op=$2&id=$3
    when i write /test/category it doesnt redirect to test/index.php?page=category but displays error link not found…
    i placed the htaccess in /test/

  • mike wrote on 29 September, 2008, 6:20

    Please ignore my previous post I accidentaly posted my previous question I deeply apologise for doing so. What I wanted to ask is thisQ: how based on the values of a variable in t he url to display a different nice URL e.g
    in http://www.mysite.com/index.php?id=110
    id id>100 (which in this example is) ot show
    http://www.mysite.com/albunm/100/
    else if id<100
    http://www.mysite.com/song/100/

  • nepal977 wrote on 21 October, 2008, 7:38

    Hi webmasters!!!!

    I have incoming url = http://www.abc.mydomain.com
    and i want to point into
    destination url= http://www.mydomain.com/abc.php
    and i need to this via htaccess
    Please help

  • Aditya wrote on 13 November, 2008, 9:57

    I am new here. I have a problem , i shall be thankfull if anybody could help me out.
    I am working on a ecommerce site, which reqires that only the product name to be visible in url, without any id or any number
    for instance
    Url for category
    http://www.domain.com/category_name/
    Url for Products
    http://www.domain.com/category_name/product_name/

    Thanks in advance

  • utsav wrote on 29 November, 2008, 16:17

    hey thankx works fine for me ( 1st one not tried other 3 ) but is working not like other tuts i found who just boast a lot of language and the script rarely work.
    One more problem.
    i need to rewrite a.php?action=show&id=10 to something like a-show-10.html
    this would be helpfull

    Thanx again

  • Echo 419 wrote on 17 December, 2008, 11:35

    first one is the best and its will work. its also important on SEO and google.
    since Google will link HTML pages than other dynamic page.

  • Echo 419 wrote on 17 December, 2008, 11:36

    first one is the best and its will work. its also important on SEO and google.
    since Google will like HTML pages than other dynamic pages link php or jsp.

  • Sathish Menon wrote on 20 December, 2008, 11:39

    Hi Roshan
    thanks for the easy steps!

    I was able to use it for my current site and hop to use in future also!
    thanks again!

  • mohit wrote on 23 December, 2008, 22:52

    In the url, I want to use .php extension instead of .html( ‘About-Us.html’) http://vebproject.com/cms/About-Us.html.

    in .htaccess file, I have used:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^([a-zA-Z0-9_-]+)\.html$ index.php?menuid=$1
    But If I change \.html$ to \.php$, no output is come.

    Would you plz help me.

    MOHIT

  • Sean McGilvray wrote on 1 January, 2009, 18:35

    I have a domain http://www.timefreedomnow.net/?username that I would like to rewrite to http://www.timefreedomnow.net/profile/username. Can anyone help me with this please?

    Thanks,

    Sean McGilvray

  • samik wrote on 12 January, 2009, 13:06

    nice example …… working fine …. thnx a lot

  • Zahid H wrote on 12 February, 2009, 5:01

    Hi,
    I’ve read all your post, and have tried some of the rules and does wonders… I am new to .htaccess, and my situation requires multiple rules to be applied
    this is how my url is
    http://www.someurl.com/page/index.php?page=Contact_US
    by following your example 5
    I know has this
    http://www.someurl.com/index.php?page=Contact_US
    I now added example 4 with 5
    but I can’t get through…
    ALL HELP WOULD BE APPRECIATED

  • irfan wrote on 17 February, 2009, 2:11

    Hi,

    I want to write user and SEO friendly URL. I want to redirect a user from this URL (http://www.developerconcepts.com/java/class/introduction.php) to this URL (http://www.developerconcepts.com/devc/introduction.php?tbl_top_tech_id=123&tbl_tech_id=454).

    Kindly let me know how to do it?

    Thank you very much in advance.

  • Tilal wrote on 20 February, 2009, 12:22

    Hello, thanks for such a great article, I need a little help can you tell me how can I display ” ‘ ” in my site url. Thanks a lot.

  • Vasim wrote on 24 February, 2009, 8:56

    hello
    i want to solve below problem :

    actual URL is :

    http://www.domain.com/dir/index.php?category=XXX&subcat=YYY

    must be become as below :

    http://www.domain.com/dir/Advetising/Books/

    thank you.

  • anusha wrote on 7 March, 2009, 11:19

    Really u r article is very easy to understand.

    I have a .htaccess file in my rootfolder. ?I want to rewrite the url as follows.
    change
    (http://www.domain.com/Home Page.php)
    to
    (http://www.domain.com/home page.php).
    i.e to rewrite my url to lower case.

    U have given 5 useful examples. Can u please help me to do this.

    I have done some code. Actually it is rewriting into lower case, but it is not redirecting.
    I got the 404 error.
    Thanks

  • Srinivas Tamada wrote on 18 March, 2009, 9:59

    Hi Roshan,

    How to write rewrite this URL using .htaccess

    http://xyz.com/abc.php?id=foxscan&page=1 to xyz.com/foxscan?page=1

  • gaurav wrote on 31 March, 2009, 19:10

    Well Roshan, i really appreciate your work.

    You have quick tutorials.

    a click from me on your sponsor’s you just got it…..

  • Sudhir wrote on 1 April, 2009, 13:51

    i find your blog is very useful. I am very new to .htaccess. By following your tutorial also i cant made it. please help me
    My current url goes like this-
    http://localhost/mypage/dashboard.php?m=services&p=services.new
    now i need to display it as-
    http://localhost/mypage/dashboard-services-servicesnew.html

    How can i do this?
    Thanks in advance

  • Web Checker wrote on 27 April, 2009, 8:47

    Hey

    This is a very good article :) Impressed. All 5 examples are extremly useful and used often.

    Thank much

    Web Checker

  • dougdo wrote on 28 April, 2009, 23:48

    Thanks for the very clear examples…regular expressions are so confusing!

    I’m using #4 as follows:
    RewriteRule ^([a-zA-Z0-9_-]+)$ rivers.htm?r=$1 [NC]
    RewriteRule ^([a-zA-Z0-9_-]+)/$ rivers.htm?r=$1 [NC]

    Which works fine except now that when I try and link to an external URL outside the site it tries to rewrite that too. My site is http://www.WhitewaterRaftingCostaRica.com — I am hoping to make it so that if the url is in the format http://www.WhitewaterRaftingCostaRica.com/sometext then it is redirected to http://www.WhitewaterRaftingCostaRica.com/rivers.htm?r=sometext (this it does) but if the URL is any other format, then it leaves it alone.

    Can anyone help?

  • tran thai wrote on 30 April, 2009, 9:38

    thank you for the useful post.

  • Shahriat Hossain wrote on 4 May, 2009, 6:15

    Nice tutorial with these 5 steps, thanks.

  • Joanne wrote on 4 May, 2009, 18:00

    I have a site set up with index.php as the main file and then all pages of the site are in a pages folder – e.g. pages/about-us.php.

    So you would browse to http://www.site.com/index.php?page=about-us and it brings up the about us page.

    I have tried using the following rewrite rule in .htaccess but with no luck:

    RewriteEngine on
    Options +FollowSymLinks
    RewriteRule (.*).html ?page=$1

    If I test it out by going to http://www.site.com/about-us.html, it comes up with Object not found error.

    Can anyone please help?

    Thanks

  • Rakesh Saini wrote on 10 May, 2009, 4:22

    thanks for tips to mod rewrite, but when i rewrite url (product.php?cat=1& subcat=2 change to product/1/2/bicycle.html ) the images of the page display none, how i rewrite mode of folder or file

  • mike wrote on 11 May, 2009, 5:14

    Have buillt this really big site and want to add htaccess the poblem is must I go to all hrefs and change index.php?a=2&x=3…etc.. to the “nice” URLs? This is pretty much work, I was wondering since with htaccess woy can put albums/12/1 and get albums.php?a=12&b=1 couldn’t this work the same way around meaning putting albums.php?a=12&b=1 and loading this page but also changing the address URL to the “nice” one?

  • pat wrote on 15 May, 2009, 2:09

    hey, i wonder how do you do when you have this :

    http://www.example.com/index.php?page=news&alpha=1

    and

    http://www.example.com/index.php?page=news&beta=2

    ??
    you see ?? it both calls the news page, but sometimes it is alpha, sometimes it is beta ??
    thankx a lot….

  • Umer wrote on 3 June, 2009, 12:45

    Hi,
    I am new user.
    I want to redirect
    http://myurl.com/profile/username
    to
    http://myurl.com/username

    I have tried this through .htaccess but CSS and connection breaks.

    Is there any other way to do this.

    Thanx in Advance.

  • tvazone wrote on 5 June, 2009, 9:51

    i think you mean for : )Rewriting product.php?id=12 to product-12.html

  • zafir wrote on 18 June, 2009, 12:12

    Dear Roshan,

    Please tell me how could i rerite url for all pages for example :www.abc.com/index.php -> http://www.abc.com/home

  • thecastex wrote on 19 June, 2009, 1:59

    Wow, thanks. Very useful for me……

  • Manish wrote on 20 June, 2009, 9:54

    Was just wondering if you could help me with a basic url rewriting query that I have got here. My Problem is

    1) the url in the browser does not redirect to the new url (though when i enter the new url myself , it works fine)
    2) Do i need to change the links in the entire site for a particular url if i need to go for a url rewriting.

  • Amar wrote on 30 June, 2009, 5:39

    The Following Is not working,,plz help me,,,,,,,,

    Rewriting product.php?id=12 to product-12.html

    It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.

    RewriteEngine on
    RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

  • WiserX wrote on 27 July, 2009, 16:11

    hi…

    This is very useful.

    Thank you dude

  • lee wrote on 28 July, 2009, 22:09

    worth noting, if you want to redirect all traffic to / to a new folder /new but you want to keep any existing subfolders of / working as they do, use this:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.test\.com$
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule (.*) /new/$1

    This redirects anything from “www.test.com” or test.com to /new/ but will keep http://www.test.com/otherproject/ going as it is.

  • Levani wrote on 1 August, 2009, 17:13

    In first tutorial if I Rewrite product.php?id=12 to product-12.html what would be shown in browser’s address bar – product.php?id=12 or product-12.html?

  • jayanta mondal wrote on 12 August, 2009, 18:46

    Hi,
    I am new user.
    I want to redirect
    http://www.mysite.com/profile/username.php?id=xxx
    to
    http://www.mysite.com/xxx

    I have tried this through .htaccess but did not solved.

    can anybody solve this problem.

    Thanx in Advance.

  • games wrote on 13 August, 2009, 9:48

    it’s good but
    if we can trance it to php script with
    RewriteRule .* /index.php

    and in php script route the given script to actions

    thank you

  • Wiwied wrote on 1 September, 2009, 0:33

    Nice tips.
    All work for me.
    Dont forget to load module rewrite at first.
    Because it’s been disable by default if you use xampp.

    Thanks a lot!

  • Giridhar wrote on 1 September, 2009, 9:23

    Hello.

    I want to rewrite my url . But I am having some problem in rewriting the url.

    my site url like this http://www.seomarketingireland.eu/main/frmIndex.php?Type=Home

    I need to rewrite the url http://www.seomarketingireland.eu/main/Home

    So please can u provide me the code???

    Waiting for reply..

    Byee and have a nice day.

  • Giridhar wrote on 1 September, 2009, 9:31

    Hi,
    I am new user.
    I want to redirect
    http://www.mytestsite.com/profile/username.php?user=xxx
    to
    http://www.mytestsite.com/profile/xxx

    I have tried this through .htaccess but did not solved.

    can anybody solve this problem.

    Thanx in Advance.

  • Web Developer wrote on 1 September, 2009, 11:09

    thanks for the article, good stuff.

  • hits wrote on 7 September, 2009, 6:50

    I am using the rule for RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1 for example.com/hits-user or example.com/hits_user it shows error but works fine with example.com/hitsuser .. can you help?

  • miguel calo wrote on 5 October, 2009, 1:53

    hi roshan, is it possible to manipulate the URL if it has 2 parameters?
    for example: localhost/folder/index.php?a=1&b=2
    or you could just link me to a tutorial. thanks.

  • raja wrote on 15 October, 2009, 11:17

    my domain name http://www.xxx.com/username , i want to rewrite into http://www.username.xxx.com/ Please help this

  • Vladimir wrote on 25 October, 2009, 0:13

    I want to ask the same. How to rewrite more than one parameter? For example “index.php?a=1&b=2″ .

    Thanks

  • 4youtop wrote on 25 October, 2009, 4:47

    Thanks for all the

  • ????? ???? wrote on 1 November, 2009, 11:23

    I thinks by using the example 3 (Redirecting non www URL to www URL) this step can help the site ranking in the search engine, because by adding this code to the htaccess the search engine will know that the two urls are referring to one site and that they are not two different sites.

  • Eiad wrote on 1 November, 2009, 23:16

    Hot Topic Thanks !

  • Sujal Shakya wrote on 6 November, 2009, 6:04

    Roshan Bro! Nice tut… I have one question regarding this htaccess…. How can we do redirect for sitename/username .

    I mean if i want to pass username sujal in website http://website.com ie http://website.com/sujal equivalent to http://website.com/main.php?username=sujal ..

    what should be done for this?

    Thanks in advance..

  • sandeep wrote on 7 November, 2009, 16:13

    Hi,

    Can u provide me the url rewriting code for my site (jspservlettutorial.info).

    Thanks.

  • shishtawitchg wrote on 10 November, 2009, 10:54

    you are great man……..!!

  • shishtawitchg wrote on 12 November, 2009, 16:11

    But how can i change my /public_html/folder to folder.mysite.com

    means i want to change my root folder to my subdomain with .htaccess

  • ehrajat wrote on 13 November, 2009, 1:17

    Nice tips.
    All work for me.
    Dont forget to load module rewrite at first.
    Because it’s been disable by default if you use xampp.

    Thanks a lot!

  • george wrote on 9 December, 2009, 7:41

    Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz

    Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

    It is working on localhost But when itried to fix it on web it not working.
    i put a test folder inside WWW root folder inside my test folder i placed neww .htaccess for user friendly URl ouside that there is error redirecting .htaccess i have early written when i test on testing folder file it redirect to error page .

    please tel me a solutions to solve this problem..

  • hussain wrote on 10 December, 2009, 9:58

    Hi i need a little bit of help. my requirement is to fix the page name with extension and variable name and the www because i want to make my URL more short
    like below url
    OLD URL:
    http://www.globalguideline.com/index.php?JScript=first_JavaScript

    NEW URL (Required):
    http://globalguideline.com/first_JavaScript

    Please guide me,
    Regards,
    Hussain

  • Rajat wrote on 10 December, 2009, 19:45

    I used mod-rewrite in htaccess to generate clean URL with base file test.php. (I am new to php)
    Initial URL:
    http://www.example.com/test.php?id=1176
    New URL:
    http://www.example.com/test/176
    Queries are appropriately passed and printed. No problem here.

    However, the same page can get displayed by the following URL also:
    http://www.example.com/strange/1176
    I have a file called strange.php in the root. test.php and .htaccess are in the same root folder.
    Though the rule is only to change test.php to test/
    it looks like any ..php file in the root directory can substitute for test and print that URL with content from 1176, as shown above. Any help?

  • belal wrote on 11 December, 2009, 11:49

    RewriteEngine on
    RewriteRule digital-camera/([^/]+)/ brand.php?brandName=$1
    RewriteRule digital-cameras/([^/]+)/([^/]+) product_list.php?bName=$1&catName=$2

    First rule is working fine but second rule is not working, can you please tell where is the problem?

    this is my website http://www.cameraforall.com
    you see it’s working when you click on canon, nikon or any brand name but it’s not working when cliking on Compact Digital Cameras/Digital SLR Cameras

    Need help, please give me advise…

  • Ranjit Kumar wrote on 17 December, 2009, 7:06

    Hi
    I have just found your site from google.com.I have problem .Problem is i have 12 links in page i want to click a particular link then in broswser it show domainname/linkname.But i want to when i click any link it should be show in browser with link name and each case run a file detail.php.So it is possible through
    .htacess file.Then send me code.
    Example
    AGRICULTURE SCIENCE ARCHAEOLOGY ARCHITECTURE suppose these are links when i click this link
    then result should be domain.com/link-name and each click detail.php file should be run.
    Thanks in advance
    Ranjit Kumar
    My english is not good.

  • Raj wrote on 6 January, 2010, 14:38

    Hi ,

    I am new to php and I am trying to follow you post but messed up my .htaccess file.And actually wants to redirect following URLs
    http://www.mysite.com/content/list?type=studio To http://www.mysite.com/list/type/studio

    and another URL with two query strings is :
    http://www.myxxxreward.com/content/content?type=category&prod=Anal&id=5 TO http://www.mysite.com/content/type/category/prod/Anal/id/5 and here type, prod and id values changes dynamically, like that I have 10 URL getting generated based on value from the database, how to make it work.

    Will you please help me out.

    Thank you
    Raj

  • Murtaza wrote on 18 January, 2010, 4:02

    Hello,

    My site is in JOOMLA. I want to rewrite the home page SEF url http://67.208.116.186:81/joomla/index.php/home to the main site http://67.208.116.186:81/joomla/. Kindly guide me which code i will put in .htaccess file? I am a newbie for using apache.

    Regards,
    Murtaza

  • gamesawy wrote on 1 February, 2010, 21:39

    thanks for the nice examples

  • dating guy wrote on 10 February, 2010, 19:59

    Really useful examples, thanks for sharing them

  • Vinidog wrote on 14 February, 2010, 19:44

    Very useful!!!

    Tks ;-)

  • Mark wrote on 17 February, 2010, 21:20

    I have tried the technique for number 4

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ page.php?page_id=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ page.php?page_id=$1

    The URL that the link in picking up is
    http://79.170.44.125/platinumbrides.co.uk/page.php?page_id=events

    I want to get it to be

    http://79.170.44.125/platinumbrides.co.uk/events

    I am getting a page not found error

    What am I doing wrong?

  • pondicherry times wrote on 22 February, 2010, 16:37

    i need to underscore for my url, how i can do it pls help me out

  • Suren wrote on 1 March, 2010, 6:52

    Hi,

    My page is http://www.sndnads.com/tv.html

    i want is like http://www.sndnads.com/tv

    Please help me out…

  • Florencia wrote on 12 March, 2010, 13:30

    This is the one example that worked for me. Thanks, very clear and usefull. Saved me lots of time.
    Regards

  • tiong wrote on 21 March, 2010, 14:44

    Very useful rewriting rules especially the no.4 been searched for.

  • puneet wrote on 31 March, 2010, 1:52

    Hi,

    I want to know how I can change
    http://www.mywebsite.com/index.php
    to
    http://www.mywebsite.com

    Thanks,
    puneet

  • Vivek wrote on 28 April, 2010, 6:40

    Hi

    i am new with .htaccess i want url
    http://dev.military-genealogy.com/nameList/ln/AA
    in place of
    http://dev.military-genealogy.com/nameList?ln=AA

    I have written following code in .htaccess file

    RewriteEngine On
    Options +FollowSymlinks
    RewriteRule nameList/ln/(.*)$ nameList.php?ln=$1

    but it is not working

    Please help me

    Thanks
    Vivek

Trackbacks

  1. 5 useful url rewriting examples using .htaccess
  2. How to re-write URLs using .htaccess | The How To Do Things Blog
  3. Fünf nützliche Beispiele für URL Umleitungen unter Benutzung der .htaccess Datei
  4. Les Geek Links de Jean Rat … (26/03/08) | T'as le bonjour de Jean Rat !
  5. March’s Best Links | Vandelay Website Design
  6. rascunho » Blog Archive » links for 2008-03-29
  7. Le blog de Vincent Battaglia » En vrak - 74
  8. How to check and enable mod_rewrite module in apache of xampp or wamp
  9. Programming Tips - PHP Regular Expressions, Image Previews, Apache Mod Rewrite and MySQL Dates » PowerDosh
  10. 5 useful url rewriting examples using .htaccess « Captain Tsubasa Logs…
  11. wiki syntax « :: unseregedanken.de …
  12. Avatud24 - ajaveeb » SEO - tasuta reklaam Googles
  13. Url Rewriting using .htaccess | Online Auction Owners
  14. unseregedanken.de … » Apache2+PHP: mod_rewrite und SEO Optimierung
  15. Using htaccess Files for Pretty URLS | Web Tutorials
  16. Using htaccess Files for Pretty URLS | KolayOnline
  17. Using htaccess Files for Pretty URLS « Webby Tutos
  18. Using htaccess Files for Pretty URLS | Webby Tutos
  19. Using htaccess Files for Pretty URLS - Webreweries.com
  20. Using htaccess Files for Pretty URLS : Webby Tutos
  21. Using htaccess Files for Pretty URLS :: CSS :: WEBDESIGN FAN
  22. Compartiendo desde delicious. 25 de Octubre – 31 de Octubre | David Táboas
  23. searchtified.com
  24. Tips Cara Merubah URL Rewriting dengan .htaccess dan Penerapannya | MataKuliah.info
Copyright © 2013 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.