且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mod_rewrite:传递路径&查询字符串 URL 作为参数

更新时间:2023-02-18 17:04:01

该查询只能通过 RewriteCondRewriteRule 只测试 URL 路径.请参阅 Jonathan Feinberg 的示例.

The query can ony be tested with RewriteCond since RewriteRule does only test the URL path. See Jonathan Feinberg’s example how to do that.

但你也可以只设置 QSA 标志,旧查询会自动附加到新查询:

But you could also just set the QSA flag and the old query gets automatically appended to the new one:

RewriteRule ^/category/([^/]+)$ /controller?category=$1 [QSA]

编辑 针对您对此问题的评论:如果您想获得初始请求的 URI 路径和查询,您需要从 请求行(THE_REQUEST变量):


Edit    In response to your comment to this question: If you want to get the initial requested URI path and query, you need to extract it from the request line (THE_REQUEST variable):

RewriteCond %{THE_REQUEST} ^[A-Z]+ ([^s]+)
RewriteRule ^/category/([^/]+)$ /controller?category=$1&_originalUrl=%1 [QSA]

但在大多数语言中,有一个环境变量具有相同的信息.

But in most languages there is an environment variable with the very same information.