问题描述
我尝试通过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';