Hide .php extension with url rewriting using .htaccess

Advertisement

Last time I’ve written an article about hiding php file extension where I’ve showed you how you can use .html or .asp extension of file instead of .php extension. But there was one flaw in that technique you have had to change the file extension explicitly but in this post I’m going to show you how to rewrite the URL instead of renaming the file extension Using this technique you will see product.html in the address bar of the browser but the actual file name remains product.php and you don’t need to rename the file extension. Furthermore you can rewrite the URL like product.php?id=5 to product-5.html.

what is the benefits of rewriting URL?

When a search engine visits the dynamic url like product.php?id=5 it does not give much importance to that URL as search engine sees “?” sign treat it as a url which keeps on changing. so we’re converting the dynamic URL like the product.php?id=5 to static url format like product-5.html. We’ll rewrite the url in such a way that in browser’s address bar it will display as a product-5.html but it actually calls the file product.php?id=5. So that why these kind of URL also named as SEO friendly URL.

what is required for URL rewriting ??

To rewrite the URL you must have the mod_rewrite module must be loaded in apache server. And furthermore, FollowSymLinks options also need to be enabled otherwise you may encounter 500 Internal Sever Error. If you don’t know much about mod_rewrite module then please check this post to know how to check and enable mod_rewrite module in apache?

Examples of url rewriting for seo friendly URL

For rewriting the URL, you should create a .htaccess file in the root folder of your web directory. And have to put the following codes as your requirement.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [nc]

The following example will rewrite the test.php to test.html i.e when a URL like http://localhost/test.htm is called in address bar it calls the file test.php. As you can see the regular expression in first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.

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

The following example will rewrite the product.php?id=5 to porduct-5.html i.e when a URL like http://localhost/product-5.html calls product.php?id=5 automatically.

Popularity: 66% [?]

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

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

132 Comments on “Hide .php extension with url rewriting using .htaccess”

  • Dave wrote on 15 February, 2008, 12:19

    The “Options +FollowSymlinks” is completely unnecessary. It allows symlinks to point outside your documentroot and has nothing at all to do with rewrites.

    If you have problems with .htaccess files not working, try checking your httpd.conf to make sure that it has “AllowOveride All” in it somewhere. Without this line, .htaccess files will not work at all.

  • Philippine Goji Juice wrote on 15 February, 2008, 12:45

    great post. i liked it…

  • Michael Hazzard wrote on 15 February, 2008, 18:30

    mod_rewrite turns your webserver into a super-server and frees it from the constraints of filenames.
    Another trick I heard of (want to try sometime) is to turn an error page into a search. For example, if someone tries to type in a wrong page say http://yourserver.com/pagethatdoesntexist.html you redirect it w/ mod_rewrite to http://yourserver.com/search.php?query=pagethatdoesntexist so that you come up w/ links to possibly what the user was looking for instead of a ‘page not found’.

  • Kuty wrote on 15 February, 2008, 22:20

    What would have to be put in .htaccess in order to rewrite both .php and .shtml extensions to .html

  • Roshan wrote on 17 February, 2008, 6:32

    for rewriting .shtml you have to define another rewrite rule and add a another line like this

    RewriteRule ^(.*)\.html$ $1.shtml [nc]

  • alex wrote on 17 February, 2008, 21:22

    very good idea Michael Hazzard, I would also like to see that happening, and I would do it myself if I knew how to!

    could someone email me at alex@kdams.es if they know how to do it please?
    thnx

  • [ Direcciones Amigales ] Oculta extension .php con .htaccess wrote on 19 February, 2008, 6:32

    [..]Muchas veces al generar consultas en php en la barra de direcciones quedan lineas completamente inteligibles (por así llamarlo de alguna forma) repletas de signos de interrogación (?) y de igual (=)[...]

  • Home Owners Insurance - HometownQuotes wrote on 21 February, 2008, 15:31

    Short and to the point. Great post!

  • g1smd wrote on 12 March, 2008, 1:11

    If you rewrite .html URLs to .php internal server filepaths you MUST also block direct access to all of the .php URLs. If you don’t block them you will soon have a massive Duplicate Content issue to sort out – with all pages of the site listed with both extensions.

  • Roshan wrote on 12 March, 2008, 4:24

    ya i absolutely agree with you…form seo point of view you must do that…

  • Dave wrote on 13 March, 2008, 12:08

    As for the custom 404 pages, what you want is:

    ErrorDocument 404 /404.php

    In either your .htaccess or your httpd.conf.

    Then just create 404.php in your DocumentRoot with your custom searching code contained within. The advantages of this over a redirect to a 404.php page are:
    1. A proper 404 HTTP code is sent back so that bots and caching agents will behave correctly
    2. The URL doesn’t change so the user can see exactly what they typed in. They will often only want to change the one letter they mis-typed.

    The relevant manual page is: http://httpd.apache.org/docs/2.0/mod/core.html#errordocument

  • Roshan wrote on 13 March, 2008, 15:49

    Thanks for the link. And you can also check the following post of mine
    http://roshanbh.com.np/2008/02/custom-error-page-htaccess.html
    for detail about custom 404 pages.

  • Lafever wrote on 21 March, 2008, 17:44

    In reply to g1smd, how would you go about blocking off access to .php files if I use this conversion from .php to .html so I don’t have to deal with the issue of duplicate content? Anyone with the answer please email me timmy0621@yahoo.com much appreciated.

  • sandy wrote on 2 April, 2008, 13:48

    Hi Friends,

    I want to create SEO friendly URLs in PHP. I know that we can do from .htaccess file but other than that do we have to make some different php script also?

    Following is my files:

    index.php

    Untitled Document

    PHP is great
    Opensocial in boom

    newsdetails.php

    <?php
    echo ” in news”;
    ?>

    .htaccess file
    # //seo_mod_start
    Options +Indexes +FollowSymlinks
    RewriteEngine On
    # Uncomment the following and add your forum path if rewrites arent working properly
    RewriteRule ^index.html$ index.php [L,NE]
    RewriteRule ^(.*)-a-([0-9]+).html$ newsdetails.php?nid=$1
    # //seo_mod_end

    But when i view index.php in browser its not showing seo friendly urls of news it shud take title of news to display.

    So the final output of url shud be php-is-great.html & opensocial-in-boom.html

    Can anybody please help me.

  • sandy wrote on 2 April, 2008, 13:51

    Friends in my above comment there is html code but i think its showing output of code. So view source and see the code.

  • tony wrote on 7 April, 2008, 6:14

    Hi Roshan, great post

  • Kamy wrote on 18 April, 2008, 14:09

    Hi:

    I created .htaccess file in my htdocs directory.
    used:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)\.htm$ $1.php[nc]

    Also in apache config file used AllowOverride All.
    Restaeted the server, still get a 404 page!

    I have a registration.php file, but I’d like to be able to browse to registration.htm file , which would display the php file.

    Am I missing a step?

    thanx

    Kamy

  • Roshan wrote on 18 April, 2008, 14:31

    Check weather mod_rewrite module is loaded or not..
    http://roshanbh.com.np/2008/04/check-enable-mod_rewrite-apache.html

    If it’s loaded then check the file name properly..
    it should work…

  • Kamy wrote on 18 April, 2008, 16:13

    mod_rewrite is enabled and in fact I got your first tutorial to work (Hiding PHP file extension).
    Rechecked everything, restarted apache…. still no go :(

  • Roshan wrote on 18 April, 2008, 16:35

    You’ve the file called registration.php file and you’re typing registration.html in browser’s address bar right ? or you’re doing it in reverse way ? can you tell me in detail ??

  • Mikey wrote on 21 April, 2008, 13:07

    Here is a successful technique to
    eliminate php file extensions in urls.

    http://www.easymodrewrite.com/example-extensions

  • Roshan wrote on 21 April, 2008, 17:30

    This post is more than hiding file extension…it basically about url rewriting…

  • Kamy wrote on 22 April, 2008, 13:27

    Hi Rishan:

    I have a file called registration.php and I’m typing registration.html in browser’s address bar right.
    I followed your steps to the letter.

    Any suggestions?

    thnax

  • Roshan wrote on 22 April, 2008, 15:56

    kamy…you are typing registration.html in browser but you’re using “.htm” in your rewrite rule not “.html”, I think that is the main problem…
    replace the above code with this

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)\.html$ $1.php[nc]

  • Kamy wrote on 22 April, 2008, 17:04

    Roshan:

    Thank you for reply.
    Still does not work!

    I checked my phpinfo file, mod-rewrite is loaded.
    I’m wondering if I have another problem? Because I tried what Mikey suggested and that did not work on my system either!!

    So I’m thinking that something is off/wrong on my system.
    As I said your initial tutorial does work here.

    VERY CONFUSDED! :( :(

    Any ideas?

    I appreciate the help. :) :)

  • Roshan wrote on 22 April, 2008, 18:00

    hey kamy…..send me zip file with that .htaccess file and that php file to bhattarairoshan[at]yahoo[dot]com

    let me see what is mistake out there

  • Kamy wrote on 23 April, 2008, 13:33

    hi Roshan:

    thank you for checking my files. :)
    I made the change , restarted Apache, still does not work !! :(

    Is there anything on Apache config I need to change perhaps?

    Kamy

  • Roshan wrote on 24 April, 2008, 4:41

    Hi kamy…which apache version you’re using? Well, everything seems to be fine in that code…when I check your files there was only one “space” missing and it worked here.

    I’ll send you the zipped working file…just unzip and put that folder inside root folder…..hope that will works

  • Kamy wrote on 24 April, 2008, 14:06

    Hi Roshan:

    Thank you for the files, but the problem STILL ESISTS!!! :(

    This is now becoming more of a puzzle then anything else.

    Running Apache 2.2.8 with PHP 5.2.5

    My gut feeling is that something in httpd.config is conflicting with the htaccess file.

    Any ideas?

    Kamy

  • Roshan wrote on 24 April, 2008, 15:13

    Well, this is the biggest puzzle I’ve ever faced. That file was perfectly working here…

    try removing Options +FollowSymlinks once…

  • Praveen wrote on 25 April, 2008, 7:40

    Hi,

    I created .htaccess file in my root directory.

    **********
    IndexIgnore */*

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)\.htm$ $1.php[nc]
    **********

    My index page is php page when i access my site i am getting this error:

    Server error!

    The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.

    If you think this is a server error, please contact the webmaster.
    Error 500

    Am I doing something wrong

    Thanks in Advance
    -Praveen

  • Roshan wrote on 25 April, 2008, 8:42

    It seems that mod_rewrite is not enabled in your apache server. Check this article to see how to check and enable mod_rewrite module….

    http://roshanbh.com.np/2008/04/check-enable-mod_rewrite-apache.html

  • Praveen wrote on 25 April, 2008, 9:50

    Hi,

    Thanks for reply it is working fine now but i have other links on the same page.
    When i access them it shows page name+.php extension.

    why?

    Thanks in Advance

  • Roshan wrote on 25 April, 2008, 10:53

    Well, you need to change the links to the “.html” or whatever your rewrite rule is there and for every different kind of url you need to define different rewrite rule

  • Praveen wrote on 25 April, 2008, 12:21

    Thanks for ur reply and support.
    It is working fine.

    -Praveen

  • Praveen wrote on 29 April, 2008, 7:21

    Hi,
    I have added this rule to hide index.php file(this is my homepage file):
    RewriteRule ^index$ index.php [L]
    RewriteRule ^about$ about.php [L]

    But this rule is not working, it is giving this error when i access this page:
    Error 404: NOT FOUND!
    Your browser cannot find the document corresponding to the URL you typed in.

    I am accessing index page like:
    Home

    I am accessing about page like:
    About Us
    This is working fine.

    Why it is not working for HomePage?

    Any help is appreciated
    Thanks in Advance
    -Praveen

  • Roshan wrote on 29 April, 2008, 16:43

    As I can see there is [L] flag which means end of the rewrite rule…just remove the [L] flag or put it at the last rewrite rule…and it will work

  • Praveen wrote on 30 April, 2008, 5:46

    Hi,
    when i access homepage i am getting this error
    Error 404: NOT FOUND!
    Your browser cannot find the document corresponding to the URL you typed in.

    I am accessing my homepage
    http://www.example.com/index

    In .htaccess file i have written:
    IndexIgnore */*
    Options +FollowSymLinks +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index$ index.php
    RewriteRule ^passenger$ passender.php
    RewriteRule ^aboutus$ aboutus.htm
    RewriteRule ^contactus$ contactus.htm

    Is there anything i need to change?

    Thanks in advance

  • Adaptiv Media wrote on 11 May, 2008, 8:42

    mod_rewrite is absolutely brilliant. if you know your regular expressions from perl or php they’re easy too!

  • Sunil wrote on 23 May, 2008, 6:38

    Hi Roshan,

    I tried your example. But its not working. According to someones reply I have to search “AllowOveride All” in httpd.conf file.

    But I didn’t found this keyword. So how to make .htacces file working. Please reply me sson.

    Byee and have a nice day.

  • Roshan wrote on 23 May, 2008, 8:44

    If it’s not here then put the following code in your httpd.conf where you’ve to specify the directoryname in the first line

    < directory “D:/xampp/xampp/htdocs” >
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
    </ directory >

  • Nilesh wrote on 19 June, 2008, 8:32

    Hi Roshan:

    I need a help. i am using htaccess file to rewrite the url’s for a project in my local server. the path to the server is something like this.

    ip/projectname/html

    all the files are inside html folder and the htaccess file also resides there. i have tried to use it for one pf the page. Below is the content of the htaccess file.

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^index_html$ index1.php[nc]
    RewriteRule ^faq$ faq.php[nc]

    when i see in browser it still shows the extension.

    mod_rewrite and other options are enabled.

    please help.

  • Roshan wrote on 19 June, 2008, 8:54

    How are you accessing that file ? What did you type in browser to access the index1.php or faq.php file ?

  • Aisha Junejo wrote on 26 July, 2008, 4:44

    Hi Roshan
    I have some problem regarding .htaccess,I have to do folder based Url rewriting in my site.I have also tried the 3rd rule that you have written.
    RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
    But when I use it on any page of my site,all the images on that page are not being displayed.
    Can you please help me a little to get out of this problem?

  • Roshan wrote on 26 July, 2008, 15:59

    yes you might face this…for this you can use “base” html element for absolute path of image and links

  • Hayden wrote on 12 August, 2008, 16:10

    Hi Roshan,

    Your example works fine when the pages are all accessed via http. However, if one of the links is pointing to a https URL e.g. user is being redirected to a secure login.html (which is translated by your rule to login.php), the rewrite rule will display a page not found error although the actual php file is there.

    How can you make this rule work on both http and https if for e.g. the HTTP host is ‘http://www.example.com‘ and the https host is ‘https://web99.secure-secure.com/example.com/'?

    Thanks in advance,

    Hayden

  • Roshan wrote on 12 August, 2008, 17:30

    As you can there is different in the urls in http and https. I think learning about regular expression may help you…

  • Hayden wrote on 13 August, 2008, 8:19

    Hi Roshan,

    Thanks for the suggestion. Can conditional statements be used in htaccess?

    e.g. if-else

    I am a newbie and I would appreciate if someone can give me some directions.

    Hayden

  • Roshan wrote on 13 August, 2008, 8:27

    yes you can use conditional statement in url writing using RewriteCond command…you can check the below post of min where I’ve used many RewriteCond command

    http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html

  • Tilal Husain wrote on 21 August, 2008, 12:37

    Hello I am designing a site on my localhost using php with apache all the things are done Moderewrite is loaded and all the other things but it is not working without any warning the home page is

    http://localhost/cutoutandkeep/homepage.php
    and I want it to be
    http://localhost/cutoutandkeep/homepage

    I have tried your code but it is not working Please help

  • PHP Web Development wrote on 10 September, 2008, 8:54

    HI,

    Will you please let me know how can I block the users so that they can’t not type .[dot] in the URL?

    THanks,

  • ashok wrote on 11 September, 2008, 13:42

    hi
    I want the code of hide the URL in GET method using in PHP
    If i send
    http://www.domain.com/script.php?mod=module&id=id
    in that i want to hide script.php?mod=module&id=id

    How can i do this
    Plese Send The Reply To me
    Thanks& Regards
    ASHOK

  • Rock wrote on 25 September, 2008, 9:31

    hello,
    Nice blog it will really helpful 4 me.but i have 1 question that i already linked my php pages means test link will link to test.php than check will link to check.php but is it possible that if i click on test link it will redirect to test.php but it will show in url test.htm directly or i will change all links from .php to .htm?

  • Rob wrote on 29 October, 2008, 3:58

    How do you enable FollowSymLinks?

  • Suvoraj wrote on 29 October, 2008, 8:41

    Superb posting. This article really helped me a lot on operating.htaccess file.

  • ismail wrote on 10 November, 2008, 8:37

    Hi Roshan..

    I’m trying to create affiliate referral program. My intention is to create a link like below:-

    http://www.mydomainname.com/username

    instead of

    http://www.mydomainname.com/?id=username

    Does your method can be applied to my program. Sorry for bad english..

  • Roshan wrote on 10 November, 2008, 16:35

    @ismail – ya ….it’s the exact solution you’re looking for

  • sahil wrote on 17 December, 2008, 11:15

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

    this is not working in my page my link is in local server
    http://linux2/bablu/admin/content.php?contentId=1

    Thanks

  • ravaz wrote on 8 January, 2009, 10:29

    how about hiding .html extension?, where to put that .htaccess file?/

  • Mauricio wrote on 16 January, 2009, 23:31

    Very nice tip.

    I was travelling around the net to do something like that.

    Thanks a lot.

    Check my website later, i`ll post some credit. Thanks again!!

    http://www.clicanuncios.com.br

  • sidix wrote on 1 February, 2009, 17:25

    Hai Rouhan…
    nice post… and I need that

    Btw, how i can make http://www.sitename.com/char.html

    The original address like this index.php?page=char

    this is my script. but is not work

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)\.html$ $1.php [nc]

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)\.html$ ^index.php?page=$1

    can you tell me about this??

    please… or send To my email…

  • subash wrote on 25 February, 2009, 12:28

    how to change index.php to index.html

    I have write :
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^index(.*)\.htm$ index.php [nc]

    in .htaccess file .
    i have removed # in httpd.conf from:

    LoadModule rewrite_module modules/mod_rewrite.so

    to load mod_rewrite module. Am i right?

    How can i solve my problem?

    thanks and regards
    Subash

  • Crishon wrote on 11 March, 2009, 15:51

    Hi guys

    I am not tech savvy at all but my webdesigners are having this problem with my Yahoo server for webhosting. Qoute “need rewrite_module to be installed in server for executing .htacces or run this code. That’s why i am telling you .htaccess module is not installed in server.

  • vantan wrote on 19 March, 2009, 13:45

    Can you give a quick htaccess example of how you would block direct access to the “.php” to prevent duplicate content from an SEO perpspective? That would be really helpful.

  • imran wrote on 26 March, 2009, 5:25

    hii roshan….can u please telll me how to enable FollowSymLinks options???

  • Sudeep Tamrakar wrote on 26 March, 2009, 6:33

    I’ve one website which is simple and static http://www.maryknollnepal.org.np/.

    I’ve use .php to include header/menu/footer for simplicity. Now I would like to show filename.html instead of .html. How is it possible?? I tried the above mentioned, not work at all. Hope you understand my problem and help me.

    thanks
    sudeep

  • mallik wrote on 26 March, 2009, 9:24

    Hi Roshan,

    I have files with the name like ’2.php’ and so on…
    in .htaccess file .
    I have write :
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^([0-9])$ $1.php [nc]

    but is not working …
    How can i solve my problem?

    thanks and regards

    Mallik

  • Hazrat Ali Khan wrote on 1 April, 2009, 10:26

    Hello
    I need help in order to hide the extension of a web page in php answer me quickly because that is the demand of my client so i have to deliver it within 5 days .Ok
    Thanks

  • coder wrote on 8 April, 2009, 17:03

    Great article, and great site, i will check and read all articles included, thanks.

  • Detectives Agency wrote on 10 April, 2009, 2:52

    can anybody tell me whats the latest PHP version ?

  • kashif wrote on 21 April, 2009, 10:51

    i m also working on that before 3 day’s i also tried above code to remove .php extension but every time i saw .php extension is on the address bar, my mod_rewrite module is enabled, and also showing module section on phpinfo page, but still problem is that i cannot able to remove .php extension is there any other way tell me Or give me any tutorial or example if any u have
    if u email me i thankful to you.

    Thankx ,
    Kashif khatri.
    k3.khatri@gmail.com

  • rizwan wrote on 9 May, 2009, 4:26

    hi roshan
    my mod_rewrite module is enabled,
    and also showing module section on phpinfo page,
    but still getting problem does not change the extension.
    let me know is any thing needed more to change at my end.
    i am using latest wamp server 2.0 having latest apache version
    my .htaccess file is :

    Options +FollowSymLinks
    Options +Indexes
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)\.htm$ $1.php[nc]
    RewriteRule ^(.*)-a-([0-9]+).html$ viewprofile.php?id=$1
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    RewriteRule ^(.*)\.htm$ index.php [nc]

  • Brusca wrote on 27 May, 2009, 6:59

    Hi Mate,

    I am trying the below htaccess

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.*)\.htm$ $1.php

    I can get the .htm files to call the equivalent .php files no problems but the extensions are not swapping out. How do I get .php to change to .htm?

    Cheers

  • coder wrote on 28 May, 2009, 20:44

    Hi brusca,
    (i think that) you are trying to put links like ****.php and you are expecting that the .htaccess make them appear like ****.htm.
    you must put links like this <a href=”***.htm”… even if your file has a ****.php extension, and the .htaccess will understand that you are pointing to ****.php .

  • Brusca wrote on 1 June, 2009, 23:09

    Thanks coder, I now have my links ?p=variable to look like directories /p/variable/. Thanks and a brilliant article!!

  • coder wrote on 2 June, 2009, 6:10

    Welcome!

  • Sudhir wrote on 12 June, 2009, 6:31

    Hi Roshan,
    You are doing a great job, but pls help me
    I have created a htacess file and wrote the below code:

    Options +Indexes +FollowSymlinks
    RewriteEngine On
    RewriteRule ^test.html$ test.php

    But the problem is the above code is working on my server but it is not working on other hosting server. Can you help me where it went wrong?

  • MatheusMK3 wrote on 18 June, 2009, 22:13

    Hello, i’m having a problem on the rewritting, is this:

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

    And yes, i have the mod_rewrite enabled, i checked the file but continues, can you help?

  • thecastex wrote on 19 June, 2009, 2:31

    I like it. Thans a lot mate…

  • Sudhir wrote on 19 June, 2009, 7:22

    Hi,
    write the below code in .htaccess file:

    Options +Indexes +FollowSymlinks
    RewriteEngine On
    RewriteRule ^test.html$ test.php
    RewriteRule ^index.html$ index.php
    RewriteRule ^products.html$ products.php
    RewriteRule ^products-([0-9]+)\.html$ products.php?productid=$1

    For example your productid=5 then test the link
    http://www.yourwebsite.com/products-5.html

    Note: .htaccess file should be in your root folder where index.php file is placed.

    Thanks,
    Sudhir

  • MatheusMK3 wrote on 19 June, 2009, 16:59

    Hello, thanks for the help, a some later, when i changed a little thing it works:

    Before:
    RewriteRule ^product-([0-9]+)\$ products.php?id=$1

    After:
    RewriteRule ^product-([0-9]+)$ products.php?id=$1

    =)

  • Sumit Joshi wrote on 27 June, 2009, 8:26

    Hi, Roshan
    I have read all above comments. I also have the same problem. I m using WAMP server in my local host. First of all please tell me that where I create .htaccess file, means in which directory? Which directory I assume root directory? In WAMP there is bin directory in www, it also contains apache, mysql and php subdirectories, so in which directory i have to create .htaccess file? See, when I execute my localhost file with PHP the my browser shows URL like “http://localhost/bliss/Login.php”. I want to rewrite this with “http://localhost/bliss/Login.html”. Please help me out this problem. I would be very thankful to you. Please…. Waiting for positive reply…please do it as soon as possible…

    Regards.
    Sumit Joshi

  • hah wrote on 29 June, 2009, 16:08

    why the hell, do every mid-skilled seo believes that url rewriting is needed in situations like in article provided example.
    Why do you think there is any diference in between product.php?id=5 and product-id5.html
    if we have hundreds of products? If it was rewriting product.php?id=5 to something like /product/washing-mashine/somemodel, then ok – i agree it has some sense in it. But again ir has no more sense than product.php?item=”washing-mashine”&model=somemodel. Modern search engines do not have any problems with “?” or “&” simbols in url. It hase sence only if you need a nice url for takeaway poster in some trade show or alike.

    But now?!!! provided example simply sux and is misleading some not very wise clients who keep asking such stupid questions like – will you hide all “?” :P
    It’s lame. Hiding only because of hiding.

    (sorry for English errors if any, it’s not y native lang)
    Cheers

  • desiz wrote on 20 July, 2009, 19:14

    hi…Roshan

    nice site i learn many php function from ur site :)

    i want to convert
    URL http://www.mysite.com/contact-us.php
    to >>> http://www.mysite.com/contact-us/

    & all other php pages like dis
    how to do ..plz mail me ur reply…thankx :)

  • Henry wrote on 23 July, 2009, 20:29

    I’ve been trying this out, but it hasnt worked. I’m a noob to playing around with PHP etc So this helped a bit with understanding .htaccess, thank you for your tutorial.

    Henry.

  • WiserX wrote on 24 July, 2009, 3:55

    hi…

    Really very useful work.

  • Julian wrote on 25 July, 2009, 13:31

    First, I just wanted to say thank you for this great read. It’s exactly what I’m looking for…mostly. I was hoping you could help me a little because I am confused and haven’t yet found a guide to successfully let me accomplish what I am looking for.

    I use a single PHP file for the entire site. I want the links to link to .html when really it is homepage.php?area=. Currently, all links point to homepage.php?area. I found this page on my search to convert all my links to look like ordinary HTML pages.

    Any further help would be greatly appreciated. Thanks!

  • Julian wrote on 25 July, 2009, 13:54

    I looked at that last comment and thought some further clarification may be needed. :)

    Let’s say I have a link to services-computers.html. I would like that to then be a page that’s true URL is homepage.php?area=services-computers. The same would be so with about.html, true URL being homepage.php?area=about. Or… contact.html is actually homepage.php?area=contact.

    My goal is to have everything pulled from include files as the entire site has the same layout and I would like to be able to easily add and remove layout features as needed.

  • brian azia wrote on 8 August, 2009, 7:29

    It is not workingggg…..

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

    thank you it’s very usful

  • sony nase wrote on 25 August, 2009, 4:01

    Thanks to your post Roshan it works can you please give another example in url rewrite.

    For Example
    in my local host i have another folder inside the another folder there is a test.php file
    so when i run it in the address bar it should be like this

    http://localhost/anotherfolder/test.php

    now i want to change it by

    http://test.php or
    http://localhost/test.php

    thanks in advance

  • Amarjeet Prasad wrote on 9 September, 2009, 9:31

    How can I rewrite url in php

  • Sunil wrote on 18 September, 2009, 13:44

    Hi! all

    *** URGENT ***
    I am trying the below htaccess

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.*)\.htm$ $1.php

    I can get the .htm files to call the equivalent .php files no problems but the extensions are not swapping out. How do I get .php to change to .htm?

    After this I tried the code like this:

    RewriteEngine on
    RewriteRule ^(.*)\.php$ $1.htm [R=302,N]

    this changes the extension of the file to .htm but with an error message

    Error 404: File Not Found

    I want to do it from the redirection only .Because if we do it by making the url as
    , then it will affect the ranking of my site.So to maintain the ranking
    I want to do it by using redirecting methods.Kindly suggest me about this.

    Regards,
    Sunil Sharma

  • Eiad wrote on 1 November, 2009, 22:53

    Hello, Thanks Very Much Really Helped Me alot.

  • 11x17 wrote on 5 November, 2009, 17:31

    TKS A LOT FOR YOUR SHARING ITS VERY USEFUL FOR SEO

  • Atheeq wrote on 18 November, 2009, 12:46

    In windows , If i put .httaccess then showing error.Kindly help me.

  • Best Electronic Cigarette Reviews wrote on 28 December, 2009, 6:24

    I hate the file extension on most of my html pages site, I will play with this one and see how it goes. Kudos for the timely info.

  • Amit wrote on 4 January, 2010, 17:47

    Hi All,

    Could you help me in rewritting url ..I am writing url rewritable rule.i want to the caetgory name and sate name change. like this http://www.abc.com/catergory name /state name/state name.html
    i want to write a rule that catergory name and state name will change … please help me

  • Mayur wrote on 5 January, 2010, 9:30

    Hi,

    I checked with all rules mentioned here and we get to convert dynamic web page query string into .html format URL you can see here http://www.addonsolutions.com/Hire-Enterprise-Java-Beans-Developer-Programmer-EJB-Developers-EJB-Programmer.html website design and development page but if can not be converted from PHP to html
    any idea?

    Thanks

  • conflicker wrote on 6 January, 2010, 10:31

    Hi Rishan..what rewriterule i can put in the htaccess if im not using id,,im trying to name for a comment purposes..

  • Facebook Developers wrote on 19 January, 2010, 8:23

    Great. I was really want to know this.

  • Rajeshkumar wrote on 27 January, 2010, 6:55

    Hi Roshan,
    I have tried according to the above instructions but i couldn’t get the result. can u help me plz plz….

    Here i am working in wamp server, so i have created a url.htaccess in www directory.
    Now i created a folder called http access, in that i created a file called product.php

    In the url.htaccess
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^product-([0-9]+)\.html$ products.php?id=$5

    in the product.php

    Name
    :

    so Finally when i want to access product-5.html, it gives the out put 404 error file not found..

    and also i have enable mod_rewrite in the conf directory .htaccess file.

    could u please help me …

  • tiosx wrote on 28 January, 2010, 20:19

    Hello, please help me, I’m making a server-side rewrite that reads foo=1 when user puts domain.com/category/, thats my code:

    Rewriterule ^category/ /?foo=1 [L]
    rewrite mod works

    But if exists a file called category.php, then rewrite mod doesn’t work

    I tried do this:

    RewriteCond %{REQUEST_FILENAME} !-f
    Rewriterule ^category/ /?foo=1 [L]
    and this:

    Rewriterule ^category/$ /?foo=1 [L]
    and also I tried this:

    Rewriterule ^category([^\.php])/$ /?foo=1 [L]
    But doesn’t work. I need help Is possible this rewrite?? or i have to delete category.php file?
    Thanks

  • iPhone Repairs - Jim wrote on 6 February, 2010, 10:38

    when using url rewriting I find it very easy on the eye without the extension part. It also makes geeks guesses hard to know whats under the hood.

  • Hair Beauty Supplies wrote on 9 February, 2010, 15:49

    I agree with Jim above, when you use URL rewriting the links to a site or a web application looks neat.

  • Abid wrote on 23 February, 2010, 18:24

    can i rewrite
    http://esportcafe.com/social/profile.php?user=test(0-9)
    into
    http://test(0-9).esportcafe.com/
    please help.

  • Jorge wrote on 25 February, 2010, 0:37

    Hi everyone!!! Fist of all I don’ write so good in English but if you understand this, may be I can help someone.

    What I found is, Firs you need a Linux hosting then create a file in notepad or any other text editor,

    Paste this code in there, save it as .htaccess that will be a .htaccess.txt upload it to the root folder of your host and then change the name for .htaccess get the “.txt” out.

    After that if you want to see the pages like http://www.yahoo.com/news/
    when you set the links in dreamweaver or any software you use just remove the .php in the link

    ej: set the link like ../news/latestnews instead of ../news/latestnews.php

    order allow,deny
    deny from all

    #Hide php extention
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^[.*]$ $1.phps

    Thank you and I hope this help.

  • Anup wrote on 13 March, 2010, 1:15

    Thanks for the great information.

  • kiran wrote on 31 March, 2010, 7:02

    hi all,
    i have problem when using dynamic url like
    RewriteRule ^refer-([0-9]+)\.html$ refer-a-friend.php?cty=$1

    This is not working for me when i given
    refer-1.html

    Can anybody help me

  • web developer wrote on 9 April, 2010, 7:37

    hi
    this is what i was looking for.
    thanx 4 sharing it.
    tried your code.
    it works fine for just converting .php to .htm.
    bt as i tried product thing its not working.
    like. for eg. url is product.php?id=5
    when i am redirecting it from a page it shows the same url like product.php?id=5 in the browser.
    can you tell me wats the wrong in it?
    thanks in advance.

  • Dhruv wrote on 21 April, 2010, 18:56

    Also is there a hack in wordpress to rewrite the default /search/ folder to /what-ever/

    thanks

    -DP

  • WEB wrote on 29 April, 2010, 3:46

    Hi Roshan,

    If i type index.php to index.html in address bar its working but its not changing automatically when i click index.php

  • Brian Brown, Ph.D. wrote on 13 May, 2010, 18:10

    Dude-
    You need to learn the difference between the words “following” and “preceding!”

  • Roshan wrote on 19 May, 2010, 4:21

    Doh!! you caught me….my bad English

  • IRSHAD wrote on 2 June, 2010, 11:47

    hi to all,

    can u give me the solution of htaccess problem.
    I have http and https both protocols and i want to redirect page if the page is demo.php it starts from https else starts from http

  • Geo wrote on 9 June, 2010, 12:47

    Hello Roshan
    After rewriting my php site with html how can I block accessing of .php links by typing directly from browser
    I have already launched my site and search engines listing my site
    So what is the solution for that
    Can I redirect all php requests to a common page like error pages

    And i didnt understand how we handle the php includes in my php pages

    Geo

Trackbacks

  1. PHP Coding School » Blog Archive » php tips [2008-02-13 14:16:29]
  2. Rabanomen » Blog Archive » (Direcciones Amigables) Oculta la extensión “.php” con .htaccess
  3. Custom Error Page with .htaccess
  4. 5 useful url rewriting examples using .htaccess
  5. How to check and enable mod_rewrite module in apache of xampp or wamp
  6. How to redirect browser to https (ssl) in php
  7. How to redirect browser to https (ssl) in php
  8. pligg.com
  9. 5 useful url rewriting examples using .htaccess | Le Kevin dot com
  10. nayakhabar.co.cc
  11. How to redirect browser to https (ssl) in php « endo – explosion kiss
  12. Url Rewrite « OnlineWebHelp
  13. URL Rewriting for PHP Web Applications Using Apache mod_rewrite extension | TechiePark
  14. Usefull tips for URL rewriting « New IT Technologies
  15. Prosoxi All about Development & Design » How to redirect browser to https (ssl) in php
  16. AuctionHub » Blog Archive » Testing Press Release
Copyright © 2013 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.