How to execute a PHP web page without the .php extension in the URL?

This is something that frameworks like CodeIgniter and Zend accomplish with ease, but can still be accomplished with just Apache's mod_rewrite, as others have suggested. Basically, use a .htaccess like this to direct all traffic to a one page:

<IfModule mod_rewrite.c>
    RewriteEngine On

        RewriteCond                     %{REQUEST_FILENAME} !-f
        RewriteCond                     %{REQUEST_FILENAME} !-d
    RewriteRule                 ^(.*)$ index.php?_url=$1 [QSA,L]
</IfModule>

Once you get on that page you can parse that _url variable, which can be whatever format you want, and handle the request appropriately.


Create .htaccess file in your web root and enter following there:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^product/([0-9]+)$ product.php?id=$1

Instead of using mod_rewrite you can also use following in your .htaccess:

 DefaultType application/x-httpd-php 

And just name your script product on the server (without .php file extension).

So you can invoke it directly and would receive any appended string as $_SERVER["PATH_INFO"]


If you don't want to use mod_rewrite (I didn't) and the other answers didn't seem to work for you (they didn't for me). Then you might try this is you're running on Apache 2.4+.

The more insecure version: make anything without an extension run as a PHP file (this isn't so bad if, like my implementation you only have a single file for the whole server, but would still leave you open to arbitrary execution if someone were able to write a file into that directory... then again, if they can write a file, they could give it a PHP extension, so not really sure if this truly broadens your exposure.)

<FilesMatch "^[^.]+$">
    ForceType application/x-httpd-php
</FilesMatch>

Put this in either an .htaccess file for your directory (assuming you have those enabled) or put it directly in the piece in your host definition.

If you want to make it more specific (and maybe more secure) you can just replace the regex with something more concrete:

<FilesMatch "^MySingleFileWithNoExension$">
    ForceType application/x-httpd-php
</FilesMatch>

Edit: You do not need regex if you want to target only one file. Use this instead

<Files "MySingleFileWithNoExension">
    ForceType application/x-httpd-php
</Files>

Then just restart Apache and you're good to go!