[英]Turn mod_rewrite rules into PHP rules
我希望能夠從 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]
有誰知道如何做到這一點?
我的方法是更換
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;
}
當然,讓.htaccess
文件看起來像您擁有的第一個示例,然后從那里構建您的腳本。
可能會編寫某種Router
class,將請求路由到它們的位置,基本原理是通過/
拆分接收到的查詢,這會為您提供一個 URL 部分的數組,然后開始調節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.