如何在 URL 中没有 .php 扩展名的情况下执行 PHP 网页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5589075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:53:50  来源:igfitidea点击:

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

phpapacheurl-rewriting

提问by Tuco

Sorry for noob question, can't understand from what I should search.

对不起,菜鸟问题,无法理解我应该搜索的内容。

I'm making a site with that page product.php?id=777
I'd like it to be product/777

我正在制作一个包含该页面的网站,product.php?id=777
我希望它是product/777

Thank you!

谢谢!

回答by arma

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

在您的 Web 根目录中创建 .htaccess 文件并在其中输入以下内容:

Options +FollowSymLinks
RewriteEngine on

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

回答by mario

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

除了使用 mod_rewrite,您还可以在 .htaccess 中使用以下内容:

 DefaultType application/x-httpd-php 

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

只需product在服务器上命名您的脚本(不带 .php 文件扩展名)。

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

所以你可以直接调用它,并会收到任何附加的字符串 $_SERVER["PATH_INFO"]

回答by Matthew Bradford

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

如果您不想使用 mod_rewrite (我没有)并且其他答案似乎对您不起作用(它们对我不起作用)。然后你可以试试这是你在 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.)

更不安全的版本:让没有扩展名的任何东西作为 PHP 文件运行(如果像我的实现一样,你只有一个文件用于整个服务器,但如果有人的话,你仍然可以任意执行能够将文件写入该目录...再说一次,如果他们可以写入文件,他们可以给它一个 PHP 扩展名,所以不确定这是否真的扩大了你的曝光率。)

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

将它放在目录的 .htaccess 文件中(假设您启用了这些文件)或直接将其放在主机定义中的部分中。

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!

然后只需重新启动 Apache,您就可以开始了!

回答by Jimmy Sawczuk

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:

这是像 CodeIgniter 和 Zend 这样的框架可以轻松完成的事情,但仍然可以mod_rewrite像其他人建议的那样仅使用 Apache 的 来完成。基本上,使用这样的 .htaccess 将所有流量定向到一个页面:

<IfModule mod_rewrite.c>
    RewriteEngine On

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

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

进入该页面后,您可以解析该_url变量,它可以是您想要的任何格式,并适当地处理请求。

回答by Simone Dagli Orti

For me this code work:

对我来说,这段代码有效:

in "/etc/apache2/sites-available/default" or "httpd.conf"

在“ /etc/apache2/sites-available/default”或“ httpd.conf”中

you must have (at least) these options in the default directory

您必须(至少)在默认目录中有这些选项

...
    <Directory />
        ...
        Options FollowSymLinks
        AllowOverride All
        ...
    </Directory>
...

OR in the site specific site directory

或在站点特定站点目录中

...
    <Directory "/var/www/my-site">
        ...
        Options FollowSymLinks
        AllowOverride All
        ...
    </Directory>
...

and create (it may not exist, make sure it is readable by apache) or edit the ".htaccess" in the directory of the site (for example "/var/www/my-site/.htaccess") and add this code:

并创建(它可能不存在,确保它可以被 apache 读取)或编辑站点目录中的“ .htaccess”(例如“/var/www/my-site/.htaccess”)并添加此代码:

AddType application/x-httpd-php htm html php
AddHandler application/x-httpd-php .htm .html

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC,L]

回答by JBH

I did this for years using the following to execute a file named "Support" (NOT Support.php, I did NOT want two ways to gain access to the file).

多年来,我一直使用以下命令来执行名为“Support”的文件(不是 Support.php,我不想通过两种方式访问​​该文件)。

<Files Support>
    SetHandler php-script
</Files>

However, I've started running into trouble with this when using Plesk to execute scripts as PHP 7.0. The root (.com/index.php) would run as PHP 7 just fine but any of my php-script files (.com/Support) would run as my version of Plesk's default PHP 5.4. I've yet to figure out why it's doing that, but it does get around the problem of setting a default handler.

但是,当我使用 Plesk 作为 PHP 7.0 执行脚本时,我开始遇到这个问题。根 (.com/index.php) 将作为 PHP 7 运行就好了,但我的任何 php-script 文件 (.com/Support) 将作为我的 Plesk 的默认 PHP 5.4 版本运行。我还没有弄清楚为什么要这样做,但它确实解决了设置默认处理程序的问题。