views:

519

answers:

3

Using Apache 2.2 and PHP 5, what's the best way to run PHP without the .php extension. For example, I have a script called app.php and I like to invoke it as

http://example.com/app

Please notice that I still want keep the .php extension to the file and I don't have mod_rewrite. Don't want use index.php either because it requires too many directories.

I did find one way by adding this to my .htaccess,

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

The page runs a little slower by using this. I suspect it invokes SSI on every PHP page. Wonder if there are any better ways to accomplish this.

A: 

The short answer is that you aren't going to be able to do this the way you want to. PHP is going to require SOME extension in order to execute the files so you might as well leave it as *.php.

Unless you use mod_rewrite you are going to have to call the files using the full file and extension.

That is the beauty of mod_rewrite--it lets you do just such a thing.

I would pose the question back to you--why can't you use mod_rewrite? Is it an environment issue, a choice, are you using Lighttpd (lighty)?

Extension

Wrap your rewrite rules in something like this to keep it from blowing up if the server doesn't support mod_rewrite:

<IfModule mod_rewrite.c>
// DO REWREITE HERE
</IfModule>

If you believe it to be a security concern or something equally valid then you could also do the following check and send them to a custom 404 or even your documentation and give them examples of how to enable mod_rewrite and explain the need, etc. This second check is optional though--if you don't use it the users will simply see the .php extension but your pages should still work.

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /application/errors/404.php
    // ERROR 404 ABOVE OR DO ANOTHER DIRECT REDIRECT TO AN INFORMATION PAGE
</IfModule>
angryCodeMonkey
For some reason, the mod_rewrite is not installed on some of the servers the site will be running on. I already got it working without extension. Just wonder if there are any faster ways which don't resort to SSI.
ZZ Coder
@ZZ Coder - Someone may have some magic for you, but I doubt it--I have tried this very thing before and never found a valid solution. Is it feasible to just let folks know that if their server doesn't support mod_rewrite then they are going to see the PHP extension? See my edit for a way to keep .htaccess from blowing up if mod_rewrite isn't enabled. That is how things like wordpress work and it does well for them.
angryCodeMonkey
+3  A: 

An alternative is to use content negotiation. Turn on multiviews:

Options +MultiViews

If a named resource doesn't exist, Apache will glob for the file, then sort based on the media type and content encoding requirements send by the browser. If there's only one file (your PHP script), then that's what the URL resolves to.

outis
It turns out mod_negotiation is installed on all the servers. So this is the best solution I found so far. There is no noticeable performance difference by doing this either. Thanks!
ZZ Coder
A: 

You could also force the mime type of a specific file in your .htaccess:

<Files app>
ForceType application/x-httpd-php
</Files>
fetasail