PHP内置在服务器中,.htaccess模块重写

xqk2d5yq  于 2022-11-16  发布在  PHP
关注(0)|答案(3)|浏览(126)

PHP的内置服务器不使用.htaccess吗?我想这是有道理的,因为它不依赖Apache(?)。无论如何,有没有可能告诉服务器使用这些文件-它可以处理URL重写吗?我有一些框架中的项目依赖于这些文件。
APPLICATION_ENV=development php -S localhost:8000 -t public/

qlzsbp2j

qlzsbp2j1#

下面是我用于内置php web服务器的路由器,如果存在文件系统中的资产,则该服务器将提供这些资产,否则将重写index.php文件。
运行方式:

php -S localhost:8080 router.php

router.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';
}
cnjp1d6j

cnjp1d6j2#

使用PHP的内置web服务器是不可能处理.htaccess的(它不依赖于apache,它完全在PHP的核心中实现)。但是,您可以使用路由器脚本(在这里描述:http://php.net/manual/en/features.commandline.webserver.php)的数据。
例如php -S localhost -S localhost:8080 router.php

oyt4ldly

oyt4ldly3#

我们目前正在处理遗留项目,我遇到了同样的问题。根据@Caleb的回答,我们设法添加了一些控件:
1.将请求路由到旧的htaccess路由器(下面示例中的url.php);
1.使用查询字符串;
1.更改当前目录以使用PHP包含;
1.另外:命名为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';
}

相关问题