PHP Code, Web

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]
Share

7 Comments

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

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

  7. Thanks, this is great, been trying to find a solution for a while now.

    I have a question though. What happens if I have mydomain.com/products and my domain.com.products/shoes, when I try to link to mydomain.com/products it gets a 403 error as it doesn't know whether to pick the file or the directory. Is there any way around this?

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image