February 16th, 2007

How to link to PHP files without the .php extension

With a corporate or business-related website, there are several advantages to being able to remove the .php file extension from any links to PHP pages on your web server.
1. It looks much cleaner and professional to have yourdomain.com/products instead of yourdomain.com/products.php
2. It's much easier from coding perspective to be able to accomplish this with files, rather than having to create lots of sub-directories with index.php files.
3. It's easier to hide the server-side technology that you're using, to prevent hacker attacks.

So, if you add one of the following two code options to your .htaccess files, you can link to your files without the .php extension. For example, you could link to /products instead or /products.php

AddHandler server-parsed .php
SetHandler application/x-httpd-php
AddHandler application/x-httpd-php .php

OR

RewriteEngine On
RewriteRule ^[^.]+$ - [T=application/x-httpd-php,L]

6 Responses to ' How to link to PHP files without the .php extension '

Subscribe to comments with RSS or TrackBack to ' How to link to PHP files without the .php extension '.

  1. mitu said,

    on March 3rd, 2007 at 11:23 pm

    i would like to use your code for removing .php extension . it's sooooooo interesting and usefull

  2. GiorgosK said,

    on March 15th, 2007 at 6:15 am

    I tried the code(s) above but it does not seem to work.

    I am using on a shared host that runs on Linux box. Tried both inside a subdirectory and in the root directory, anybody has an altrenative ?

  3. Justin Cook said,

    on March 15th, 2007 at 7:18 am

    Maybe contact your host and ask them why it doesn't work in your environment. When you find out the solution, please share it with us!


  4. on April 12th, 2007 at 7:52 am

    As per this post, the code is not working for me either. Don't know what ar e the mistakes. its just not working. But thanks for sharing it anyway.

  5. Gabe said,

    on May 24th, 2007 at 7:08 am

    Another use for this is creating Search Engine Optimized URLs. search: SEF URLs or URL Rewriting.

    It's easier to hide the server-side technology that you're using, to prevent hacker attacks.

    Hiding the server-side technology is to client-server network security what obfuscation is to encryption. (Quite useless)

  6. Ryan said,

    on June 12th, 2007 at 4:55 pm

    One other benefit is if you change what server side language you're using. My first dynamic site was written using ASP. Eventually I taught myself PHP and preferred it to ASP so I wanted to rewrite that site in PHP. But all the pages were indexed as .ASP. Had I done something like this it would have been an easier switch. What I wound up doing was about 100 301 redirects which worked, but it took me a while to put the list of old links together.

Leave a reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image