如何处理内置 PHP 服务器的 URL 中带有点的回调?

How to deal with callbacks having dot in the URL with built-in PHP server?

我在内置 PHP 开发服务器(运行 作者:php -S localhost:8888)中使用 Drupal 站点(使用 .htaccess 和干净的 URL,它们工作正常)在 CMS 中,我动态生成我想公开的 XML 文件,但是当我尝试打开 /foo/bar.xml 时,会发生以下错误:

The requested resource /foo/bar.xml was not found on this server.

经过调查,似乎PHP内置服务器假定所有包含点的文件都必须是本地文件系统中的文件。

有什么解决方法吗?

根据PHP Built-in web server docs,要处理自定义请求,您需要指定一个"router"脚本(在命令行上给出),其中returns FALSE,然后返回请求的资源 as-is,否则脚本的输出返回给浏览器。

使用路由器脚本

这是一个简单的例子,它处理图像请求,然后显示它们,否则如果请求 HTML 个文件,它将显示 "Welcome to PHP":

<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
    return false;    // serve the requested resource as-is.
} else { 
    echo "<p>Welcome to PHP</p>";
}
?>

Drupal 7 和 8

对于 Drupal 7 和 8,您可以使用 .ht.router.php 文件,然后 运行 如下:

php -S localhost:8888 .ht.router.php