Hide .php extension with url rewriting using .htaccess

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.

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

  1. 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,

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

  3. Rock

    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?

  4. Rob

    How do you enable FollowSymLinks?

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

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

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

  8. sahil

    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

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

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

  11. sidix

    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…

  12. subash

    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

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

  14. vantan

    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.

  15. imran

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

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

  17. mallik

    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

  18. Hazrat Ali Khan

    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

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

  20. can anybody tell me whats the latest PHP version ?

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

  22. rizwan

    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]

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

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

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

  26. Welcome!

  27. Sudhir

    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?

  28. 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?

  29. I like it. Thans a lot mate…

  30. Sudhir

    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

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

    =)

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

  33. hah

    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 “?” 😛
    It’s lame. Hiding only because of hiding.

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

  34. desiz

    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 :)

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

  36. hi…

    Really very useful work.

  37. 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!

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

  39. brian azia

    It is not workingggg…..

  40. thank you it’s very usful

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

  42. How can I rewrite url in php

  43. Sunil

    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

  44. Hello, Thanks Very Much Really Helped Me alot.

  45. TKS A LOT FOR YOUR SHARING ITS VERY USEFUL FOR SEO

  46. Atheeq

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

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

  48. Amit

    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

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

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

Comments are closed.