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. Great. I was really want to know this.

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

    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 …

  3. 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?
    Thanks

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

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

  6. Abid

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

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

  8. Thanks for the great information.

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

  10. web developer

    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.

  11. Dhruv

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

    thanks

    -DP

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

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

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

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

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

    Geo

Comments are closed.