.htaccess mod_rewrite 规则以匹配干净的 URL 以及其他自定义参数

.htaccess mod_rewrite rule to match the clean URL as well as additional custom parameters

这是我为我的 REST API 找到的 .htaccess 文件,它以这种方式重写 URL:rest.php/a/b/c/... - > rest.php?url=a/b/c/...

RewriteEngine On
RewriteRule ^(.*)$ rest.php?url= [L,NC,NE]

我一直在尝试修改此代码以允许额外的 GET 参数,因为在当前形式中它只是忽略它们并且 PHP 只看到 'url'。这怎么可能?

我想要这样的东西:rest.php/a/b/c/...?filter=word -> rest.php?url=a/b/c/...&filter=word

您需要标志 QSA(查询字符串追加)

RewriteEngine On
RewriteRule ^(.*)$ output.php?url= [QSA,NC,NE]

给我 url

http://localhost/rest.php/a/b/c/?var2=val2

以下GET个参数

array(2) { ["url"]=> string(15) "rest.php/a/b/c/" ["var2"]=> string(4) "val2" } 

有关详细信息,请参阅 Rewrite Flag QSA