PHP开发服务器中MODx的友好URL

问题描述

我尝试通过php -S localhost:8000在本地运行基于modx的网站的副本。一切正常,除了/mypage之类的友好url返回索引页的内容(这是404页的默认modx行为)。我尝试使用路由器(php -S localhost:8000 router.php),但是它不起作用。

<?php
    /*  Friendly Urls
        ================================================
        RewriteEngine On
        RewriteCond %{SCRIPT_FILENAME} !-f [NC]
        RewriteCond %{SCRIPT_FILENAME} !-d [NC]
        RewriteRule ^(.+)$ /index.php?q=$1 [QSA,L]
        ================================================ */

    $root=__dir__;

    $uri=parse_url($_SERVER['REQUEST_URI'])['path'];
    $q=trim($uri,'/');   

    if (file_exists("$root/$q") && is_file("$root/$q")) {
        return false; // serve the requested resource as-is.
        exit;
    }

    $_GET['q']=$q;
    require_once 'index.php';
?>

如何解决?

解决方法

经过一些调试后,我将原始代码转换为这一代码,这似乎是正确的:

<?php
/*  Friendly Urls
    ================================================
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-f [NC]
    RewriteCond %{SCRIPT_FILENAME} !-d [NC]
    RewriteRule ^(.+)$ /index.php?q=$1 [QSA,L]
    ================================================ */

$root = __DIR__;

$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$path = ltrim($uri,'/');

if (file_exists("$root/$path")) {
    return false;  // serve the requested resource as-is.
    exit;
}

$q = urldecode($path);

$_GET['q'] = $q;
$_REQUEST['q'] = $q;

require_once 'index.php';

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...