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

时间:2021-11-16 11:03:52

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 porjects in frameworks that rely upon these files.

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

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

APPLICATION_ENV =开发php -S localhost:8000 -t public /

2 个解决方案

#1


11  

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:

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

#2


8  

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

使用PHP内置的webserver无法处理.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

#1


11  

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:

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

#2


8  

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

使用PHP内置的webserver无法处理.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