使用 n 个参数的 Apache URL 重写
我正在做一些 URL 重写,我可以通过以下方式完成我需要做的事情:
RewriteRule ^([^/]*)/([^/]*)$ index.php?arg1=$1&arg2=$2 [L,QSA]
RewriteRule ^([^/]*)$ index.php?arg1=$1 [L,QSA]
你明白了我在这里想做的事情,我基本上想获取完整的 url 并将其解析为单独的参数。所以 /blah/test/asdf/hi 将以 arg1=blah、arg2=test 等形式出现。
现在,如果我想将其扩展为 4 或 5 个参数,我想知道是否有一种更简洁的方式来呈现它比出去 ?arg1=$1&arg2=$2&arg3=$3 等等。我见过人们以编程方式执行此操作(只需用斜杠获取整个字符串并在代码中解析它),但我很好奇是否有一种用 apache 来做的方法。
I'm doing some URL rewriting and I can accomplish what I need to do with the following:
RewriteRule ^([^/]*)/([^/]*)$ index.php?arg1=$1&arg2=$2 [L,QSA]
RewriteRule ^([^/]*)$ index.php?arg1=$1 [L,QSA]
You get what I'm trying to do here, I basically want to take the full url and parse it into individual arguments. So /blah/test/asdf/hi would come in as arg1=blah, arg2=test, etc.
Now, if I want to expand this to 4 or 5 arguments, I'm wondering if there's a cleaner way to present it rather than going out with ?arg1=$1&arg2=$2&arg3=$3 etc. etc. I've seen people do this programatically (just take whole string with slashes and parse it in code) but I was curious if there was a way to do it with apache.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 PHP 来执行此操作:
第一行仅从请求 URL 获取 URL 路径。另一行将删除前导斜杠 (
substr
),分成段 (explode
) 并解码每个段 (array_map
和rawrurldecode
)。现在您只需将所有请求传递到您的 index.php:
附加条件将排除可以映射到现有文件的请求。
You can use PHP to do that:
The first line is to get just the URL path from the request URL. The other line will remove the leading slash (
substr
), split into the segments (explode
) and decode each segment (array_map
withrawrurldecode
).Now you just need to pass all request to your index.php:
The additional condition will exclude requests that can be mapped to existing files.