PHP 内置服务器和 .htaccess mod 重写


PHP built in server and .htaccess mod rewrites

PHP的内置服务器不使用.htaccess吗?我想这是有道理的,因为它不依赖于Apache(?无论如何,是否可以告诉服务器使用这些文件 - 它可以处理 URL 重写吗?我在依赖于这些文件的框架中有一些项目。

APPLICATION_ENV=development php -S localhost:8000 -t public/

这是我用于内置 php Web 服务器的路由器,它为文件系统中的资产(如果存在)提供服务,否则对索引.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';
}

使用PHP的内置Web服务器处理.htaccess是不可能的(它不依赖于apache,它完全在PHP的核心中实现)。但是,您可以使用路由器脚本(在此处描述:http://php.net/manual/en/features.commandline.webserver.php)。

例如 php -S localhost -S localhost:8080 router.php

我们目前正在处理遗留项目,我遇到了同样的问题。根据@Caleb的回答,我们设法添加了更多控件:

  1. 将请求路由到旧的 htaccess 路由器(url.php以下示例中
  2. );
  3. 使用查询字符串;
  4. 更改当前目录以使用 PHP 包含;
  5. 另外:命名为server.php以匹配Laravel的PHP内置路由器。

只需输入cmd:php -S localhost:8888 server.php

chdir(__DIR__);
$queryString = $_SERVER['QUERY_STRING'];
$filePath = realpath(ltrim(($queryString ? $_SERVER["SCRIPT_NAME"] : $_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 (behind) of this directory for security
    // 2. check for circular reference to server.php
    // 3. don't serve dotfiles
    if (strpos($filePath, __DIR__ . DIRECTORY_SEPARATOR) === 0
        && $filePath != __DIR__ . DIRECTORY_SEPARATOR . 'server.php' 
        && substr(basename($filePath), 0, 1) != '.'
    ) {
        if (strtolower(substr($filePath, -4)) == '.php') {
            // change directory for php includes
            chdir(dirname($filePath));
            // 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 router file
    // this portion should be customized to your needs
    $_REQUEST['valor'] = ltrim($_SERVER['REQUEST_URI'], '/');
    include __DIR__ . DIRECTORY_SEPARATOR . 'url.php';
}