将默认 index.php/index.html 更改为 PHP 服务器上的其他内容


Change the default index.php/index.html to something else on PHP Server

我正在使用PHP附带的服务器环境作为我的开发服务器。即我正在使用以下内容来运行网站。

php -S localhost:3000

我希望它查找索引.php或索引以外的其他内容.html作为特定文件夹(如 source.html)的默认值。我知道我可以在Apache上做到这一点。但是有没有办法对上面提到的PHP服务器做同样的事情?

不要编辑你的 Apache 配置文件(如 .htaccess)。PHP 内置服务器不使用它们(除非您使用第三方解决方案)。

你试过php -S localhost:3000 source.html吗?

或者,当您的索引位于另一个目录中时:

php -S localhost:3000 -t docroot docroot'source.html

使用前端控制器模式:

在更高级的用例中,您可能希望将除静态图像、js 和 css 文件之外的所有请求路由到前端控制器(例如 index.php),您需要创建一个单独的路由脚本 router.php

if (preg_match('/'.(?:png|jpg|jpeg|css|js)$/', $_SERVER['REQUEST_URI'])) {
    return false;
} else {
    include __DIR__ . '/index.php';
}

然后运行它:

php -S localhost:3000 -t docroot docroot'router.php

重写请求以使用默认索引文件:

好的,我认为您想要的是在其原始位置服务器每个文件。只有在访问目录时,该目录中的source.html才应用作默认文件。

将路由器.php更改为:

if (is_dir(__DIR__ . $_SERVER['PHP_SELF'])) {
    include __DIR__ . $_SERVER['PHP_SELF'] . '/source.html';
} else {
    // Try to load file directly from disk.
    return false;
}

你可以在文件httpd-vhosts.conf上更改它

这里是您的虚拟主机文档根等的配置

<IfModule dir_module>
    DirectoryIndex index.php source.html test.php
</IfModule>

或者你可以在文件 http.conf 中做同样的事情