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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27381520/
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 23:20:04  来源:igfitidea点击:

PHP built in server and .htaccess mod rewrites

php.htaccessphp-5.4

提问by Martyn

Does PHP's built in server not make use of .htaccess? Makes sense, I suppose, as it isn't relying upon Apache(?). Anyway, is it possible to tell the server to make use of these files - can it handle URL rewrites? I have some projects in frameworks that rely upon these files.

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

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

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

回答by Caleb

Here's the router that I use for the builtin php webserver that serves assets from the filesystem if they exist and otherwise performs a rewrite to an index.php file.

这是我用于内置 php 网络服务器的路由器,它从文件系统提供资产(如果存在),否则会重写 index.php 文件。

Run using:

运行使用:

php -S localhost:8080 router.php

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';
}

回答by Agares

It is not possible to handle .htaccess using PHP's built-in webserver (it is not relying on apache, it is implemented entirely in PHP's core). However, you can use router script (described here: http://php.net/manual/en/features.commandline.webserver.php).

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

E.g. php -S localhost -S localhost:8080 router.php

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