0

我希望能够从 PHP 而不是我的 .htaccess 文件中处理 mod 重写,这样当我有需要新重写的自定义模块时,我不必重做 .htaccess 文件。

我想模仿 WordPress 执行 .htaccess 的方式,即:

RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]

我当前的 .htaccess 是这样的:

Options +FollowSymlinks
RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# MBP Rules
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&page=$3 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&id=$3 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/?$ /index.php?p=$1 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=$1&s=$2 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=$1&s=$2&id=$3 [NC,QSA,L]

有谁知道如何做到这一点?

4

2 回答 2

2

我的方法是更换

RewriteRule ^(.+)$ /index.php/$1 [L,QSA]

RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]

然后你必须像 modrewrite 那样做类似的解析和所有事情,但你可以使用 $_GET['path'] 上的 preg_match 自己做。IE

if (preg_match('/id/([0-9]*)', $_GET['path'], $matches)) {
    Do code;
}
于 2012-04-09T20:04:20.210 回答
1

当然,让.htaccess文件看起来像您拥有的第一个示例,然后从那里构建您的脚本。

可能会编写某种Router类,将请求路由到它们的位置,基本原理是将接收到的查询拆分为/,这为您提供了 URL 部分的数组,然后开始调节。

于 2012-04-09T20:02:45.433 回答