如何 mod 正确重写这个 URL?

How to mod rewrite this URL correctly?

我正在向我的应用程序添加 REST API。 REST API 位于我的应用程序的 /api 。我想正确路由所有请求,包括具有参数的请求。我有以下大部分工作的 htaccess 规则:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url= [L]
</IfModule>

使用这些规则,我现在可以去: mysite.com/api/projects/get 并且它会正确地返回 JSON 数据,就好像我会以常规方式访问它一样 mysite.com?url=api/projects/get

当我尝试向请求添加参数时,我的问题就开始了。 我的应用程序将已经处理参数。所以如果我去:mysite.com/api/projects&type=1,它会给我返回类型 1 的项目。但是,我认为 URL 看起来更像是:mysite .com/api/projects?type=1(注意 ? 而不是 &)

我该如何修改我的 htaccess 规则来处理这个问题?

我正在尝试以下但没有成功:

RewriteRule ^(.*)/?(.*)$ index.php?url=& [L]

您需要使用查询字符串附加标志,如下所示:

RewriteRule ^(.*)$ index.php?url= [QSA,L]

根据 documentation:

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Consider the following rule:

RewriteRule "/pages/(.+)" "/page.php?page=" [QSA]

With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.