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. thank you it’s very usful

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


    now i want to change it by

    http://test.php or

    thanks in advance

  3. How can I rewrite url in php

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

    Sunil Sharma

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


  7. Atheeq

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

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

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

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


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

  12. Great. I was really want to know this.

  13. Rajeshkumar

    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


    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 …

  14. tiosx

    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?

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

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

  17. Abid

    can i rewrite
    please help.

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

  19. Thanks for the great information.

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

    Can anybody help me

  21. web developer

    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.

  22. Dhruv

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



  23. WEB

    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

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

  25. Doh!! you caught me….my bad English

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

  27. Geo

    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


Comments are closed.