Apache 重写会消耗一级转义 (%23)
我想在我的网站上使用精美的 URL 进行标签过滤器。 URL 应类似于 http://example.com/source/+tag1+tag2。这应该过滤所有带有 tag1 和 tag2 标记的项目。我为此提出了以下重写规则,并将其保存到站点的根目录中:
RewriteRule ^([^+]+)(\+.+)$ $1?tags=$2 [L]
这对于所有普通标签名称都适用,但对于标签名称“c#”则失败。我知道哈希字符没有发送到服务器,所以标签名称是url编码的,HTML页面中的链接是这样的: ./+c%23
但目标页面会只能在其标签参数中看到“c”,其余部分以及“#”之后的任何内容都不再存在。
我启用了 Apache 的重写日志记录,发现它已经记录了传入的 URL 请求,例如 …/+c#
。这让我认为可能需要另一个级别的转义。所以我尝试使用%2523,它实际上成功地通过了重写,并且整个字符串“c#”出现在我的页面中。
但话又说回来,当我使用 ./?tags=c%23
等内部 URL 访问该页面时,它也已经可以工作了。那么为什么 Apache 会吞噬一级转义呢?我可以使用隐藏的重写标志来避免这种情况吗?我是否需要使用双重编码的公共 URL 才能使我的奇特 URL 正常工作?或者它会太混乱,我应该将我的标签重命名为“csharp”?
I want to use fancy URLs for a tag filter on my website. The URLs should look like http://example.com/source/+tag1+tag2. This should filter for all items tagged with tag1 and tag2. I came up with the following rewrite rule for that, which I have saved to the root directory of the site:
RewriteRule ^([^+]+)(\+.+)$ $1?tags=$2 [L]
This works fine for all normal tag names, but it fails for the tag name "c#". I know that the hash character is not sent to the server, so the tag name is url-encoded and the link in the HTML page is like this: ./+c%23
But the target page will only see the "c" in its tags parameter, the rest and anything after the "#" is not there anymore.
I have enabled Apache's rewrite logging and saw that it already logs the incoming URL request like …/+c#
. This made me think that another level of escaping could be required. So I tried with %2523 which actually passed the rewriting successfully and the whole string "c#" turned up in my page.
But then again, when I access the page with its internal URL like ./?tags=c%23
, it already works, too. So why is Apache eating up one level of escaping? Is there a hidden rewrite flag I can use to avoid that? Do I need to use public URLs that are double-encoded for my fancy URLs to work? Or will it be too messy and I should instead just rename my tag to "csharp"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你需要 B 标志 (所以使用 [L, B])
I think you need the B flag (so use [L,B])