Running PHP without extension without using mod_rewrite?

让人想犯罪 __ 提交于 2019-12-18 11:53:27

问题


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.


回答1:


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.




回答2:


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

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



回答3:


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>


来源:https://stackoverflow.com/questions/2358178/running-php-without-extension-without-using-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!