Hide .php extension with url rewriting using .htaccess

Posted on February 13, 2008 
Filed Under htaccess, php, tips and technique, tutorial

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: 100% [?]

If you like this post then please subscribe to my full RSS feed . You can also subscribe by email and have new posts sent directly to your inbox.And, You can also follow me on twitter at http://twitter.com/roshanbh.

Related Posts

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

Comments

53 Responses to “Hide .php extension with url rewriting using .htaccess”

  1. PHP Coding School » Blog Archive » php tips [2008-02-13 14:16:29] on February 13th, 2008 2:21 pm

    [...] Hide .php extension with url rewriting using .htaccess By Roshan 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 … Roshan Bhattarai’s Blog - PHP… - http://roshanbh.com.np [...]

  2. Dave on February 15th, 2008 12:19 pm

    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.

  3. Philippine Goji Juice on February 15th, 2008 12:45 pm

    great post. i liked it…

  4. Michael Hazzard on February 15th, 2008 6:30 pm

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

  5. Kuty on February 15th, 2008 10:20 pm

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

  6. Roshan on February 17th, 2008 6:32 am

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

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

  7. alex on February 17th, 2008 9:22 pm

    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

  8. Rabanomen » Blog Archive » (Direcciones Amigables) Oculta la extensión “.php” con .htaccess on February 18th, 2008 2:46 pm

    [...] Fuente | Roshan [...]

  9. [ Direcciones Amigales ] Oculta extension .php con .htaccess on February 19th, 2008 6:32 am

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

  10. Custom Error Page with .htaccess on February 21st, 2008 5:12 am

    [...] my last post of Hiding .php file extension with .htaccess Michael has mentioned that you can create your own custom page for the the 404(not found) error in [...]

  11. Home Owners Insurance - HometownQuotes on February 21st, 2008 3:31 pm

    Short and to the point. Great post!

  12. 5 useful url rewriting examples using .htaccess on March 11th, 2008 4:10 am

    [...] If you are looking for the examples of URL rewriting then this post might be useful for you. In this post, I’ve given five useful examples of URL rewriting using .htacess. If you don’t know something about url rewriting then please check my older post about url rewriting using .htaccess. [...]

  13. g1smd on March 12th, 2008 1:11 am

    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.

  14. Roshan on March 12th, 2008 4:24 am

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

  15. Dave on March 13th, 2008 12:08 pm

    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

  16. Roshan on March 13th, 2008 3:49 pm

    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.

  17. Lafever on March 21st, 2008 5:44 pm

    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.

  18. sandy on April 2nd, 2008 1:48 pm

    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.

  19. sandy on April 2nd, 2008 1:51 pm

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

  20. How to check and enable mod_rewrite module in apache of xampp or wamp on April 7th, 2008 6:02 am

    [...] apache after reading two most of the popular post of this blog 5 useful url rewriting examples and hide .php extension with url rewriting. How to check weather mod_rewrite module is enabled or not? Well there are lots of technique to [...]

  21. tony on April 7th, 2008 6:14 am

    Hi Roshan, great post

  22. Kamy on April 18th, 2008 2:09 pm

    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

  23. Roshan on April 18th, 2008 2:31 pm

    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…

  24. Kamy on April 18th, 2008 4:13 pm

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

  25. Roshan on April 18th, 2008 4:35 pm

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

  26. Mikey on April 21st, 2008 1:07 pm

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

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

  27. Roshan on April 21st, 2008 5:30 pm

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

  28. Kamy on April 22nd, 2008 1:27 pm

    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

  29. Roshan on April 22nd, 2008 3:56 pm

    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]

  30. Kamy on April 22nd, 2008 5:04 pm

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

  31. Roshan on April 22nd, 2008 6:00 pm

    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

  32. Kamy on April 23rd, 2008 1:33 pm

    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

  33. Roshan on April 24th, 2008 4:41 am

    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

  34. Kamy on April 24th, 2008 2:06 pm

    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

  35. Roshan on April 24th, 2008 3:13 pm

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

    try removing Options +FollowSymlinks once…

  36. Praveen on April 25th, 2008 7:40 am

    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

  37. Roshan on April 25th, 2008 8:42 am

    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

  38. Praveen on April 25th, 2008 9:50 am

    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

  39. Roshan on April 25th, 2008 10:53 am

    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

  40. Praveen on April 25th, 2008 12:21 pm

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

    -Praveen

  41. Praveen on April 29th, 2008 7:21 am

    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

  42. Roshan on April 29th, 2008 4:43 pm

    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

  43. Praveen on April 30th, 2008 5:46 am

    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

  44. How to redirect browser to https (ssl) in php on May 2nd, 2008 12:12 pm

    [...] Well, just copy and paste the above code in .htaccess file then the whole website will be redirected to “https” when the browser is opened in “http” mode. The browser just get redirected using url rewriting in .htaccess. [...]

  45. How to redirect browser to https (ssl) in php on May 3rd, 2008 5:37 pm

    [...] Just copy and paste the above code in .htaccess file then the whole website will be redirected to “https” when the browser is opened in “http” mode. The browser just get redirected using url rewriting in .htaccess. [...]

  46. Adaptiv Media on May 11th, 2008 8:42 am

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

  47. Sunil on May 23rd, 2008 6:38 am

    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.

  48. Roshan on May 23rd, 2008 8:44 am

    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 >

  49. Nilesh on June 19th, 2008 8:32 am

    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.

  50. Roshan on June 19th, 2008 8:54 am

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

  51. pligg.com on July 10th, 2008 1:29 pm

    Rescrierea url-urilor cu .htaccess…

    Converia din URL dinamic in Url static…

  52. Aisha Junejo on July 26th, 2008 4:44 am

    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?

  53. Roshan on July 26th, 2008 3:59 pm

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

Leave a Reply