PHP 的内置服务器不使用 .htaccess 吗?我认为这是有道理的,因为它不依赖 Apache(?)。不管怎样,是否可以告诉服务器使用这些文件——它可以处理 URL 重写吗?我的框架中有一些依赖这些文件的项目。
APPLICATION_ENV=development php -S localhost:8000 -t public/
这是我用于内置 php Web 服务器的路由器,该路由器为文件系统中的资产(如果存在)提供服务,否则执行对 index.php 文件的重写。
运行使用:
php -S localhost:8080 router.php
路由器.php:
<?php
chdir(__DIR__);
$filePath = realpath(ltrim($_SERVER["REQUEST_URI"], '/'));
if ($filePath && is_dir($filePath)){
// attempt to find an index file
foreach (['index.php', 'index.html'] as $indexFile){
if ($filePath = realpath($filePath . DIRECTORY_SEPARATOR . $indexFile)){
break;
}
}
}
if ($filePath && is_file($filePath)) {
// 1. check that file is not outside of this directory for security
// 2. check for circular reference to router.php
// 3. don't serve dotfiles
if (strpos($filePath, __DIR__ . DIRECTORY_SEPARATOR) === 0 &&
$filePath != __DIR__ . DIRECTORY_SEPARATOR . 'router.php' &&
substr(basename($filePath), 0, 1) != '.'
) {
if (strtolower(substr($filePath, -4)) == '.php') {
// php file; serve through interpreter
include $filePath;
} else {
// asset file; serve from filesystem
return false;
}
} else {
// disallowed file
header("HTTP/1.1 404 Not Found");
echo "404 Not Found";
}
} else {
// rewrite to our index file
include __DIR__ . DIRECTORY_SEPARATOR . 'index.php';
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)