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

  2. great post. i liked it…

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

  4. Kuty

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

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

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

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

  7. [..]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 (=)[…]

  8. Short and to the point. Great post!

  9. g1smd

    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.

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

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

  12. Thanks for the link. And you can also check the following post of mine
    for detail about custom 404 pages.

  13. Lafever

    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.

  14. 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:


    Untitled Document

    PHP is great
    Opensocial in boom


    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.

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

  16. Hi Roshan, great post

  17. Hi:

    I created .htaccess file in my htdocs directory.
    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?



  18. Check weather mod_rewrite module is loaded or not..

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

  19. 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 :(

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

  21. Mikey

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


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

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


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

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


    Any ideas?

    I appreciate the help. :) :)

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

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


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

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


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

    try removing Options +FollowSymlinks once…

  31. Praveen


    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

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


  33. Praveen


    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.


    Thanks in Advance

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

  35. Praveen

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


  36. Praveen

    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:

    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

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

  38. Praveen

    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

    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

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

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

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

  42. Nilesh

    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.


    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.

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

  44. Aisha Junejo

    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?

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

  46. Hayden

    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,


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

  48. Hayden

    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.


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


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

    and I want it to be

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

Comments are closed.