12

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

ata
  • 3,398
  • 5
  • 20
  • 31
ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
  • I have seen this done using a `` directive but you're much less likely to get that running in a shared environment than `mod_rewrite` I think. – Pekka Mar 01 '10 at 19:01
  • 2
    Personally, I'd just drop enough hints about "degrading performance" and "I'm givin' her all she's got, Captain!" until they installed mod_rewrite. – Xorlev Mar 01 '10 at 22:15

3 Answers3

25

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
  • 75,655
  • 22
  • 151
  • 221
  • 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 Mar 02 '10 at 00:13
  • 2
    This is still relevant, and you may need to add "AddType application/x-httpd-php .php .phtml .html .htm" to "/etc/apache2/mods-enabled/mime.conf:" – mckenzm Mar 06 '15 at 22:02
  • 1
    @mckenzm: .html and .htm files shouldn't be interpreted as PHP. `AddType` should only be used to set the MIME type of a resource, not a handler. For that, there's `AddHandler` (which even works when PHP is run through CGI). – outis Aug 07 '15 at 00:10
7

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

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

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>
Shane
  • 16,779
  • 5
  • 27
  • 46
  • 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 Mar 01 '10 at 18:55
  • @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. – Shane Mar 01 '10 at 18:58