如何用 %23 重写 URL? [英] How to rewrite a URL with %23 in it?
问题描述
我有一个(wordpress)博客,评论后用户被重定向回页面,并带有指向他们评论的锚点.应该是这样的:
I have a (wordpress) blog where after commenting the users are redirected back to the page with an anchor to their comment. Should look like this:
http://example.org/foo-bar/#comment-570630
但不知何故,我在我的日志文件中为此类 URL 获取了很多 404:
But somehow I get a lot of 404 ins my logfiles for such URLs:
http://example.org/foo-bar/%23comment-570630
有没有办法编写 .htaccess 重写规则来解决这个问题?
Is there a way to write a .htaccess rewrite rule to fix this?
奖金问题:知道为什么会发生这种情况以及我能做些什么吗?
Bonus question: Any idea why this happens and what I can do about it?
推荐答案
%23
是 #
的 URL 编码表示.我怀疑您的重写规则不满足 %23
.您应该调查响应是如何构建的.具体来说,任何 URL 编码函数.
%23
is the URL encoded representation of #
. I suspect your rewrite rules will not satisfy %23
. You ought to investigate how the response is being constructed. Specifically, any URL encoding functions.
但是,可以通过重写规则解决您的问题.了解提交评论后,您将向客户返回两个响应.这就是为什么最好更正第一个响应的原因.
However, it would be possible to solve your issue with a rewrite rule. Understand that you'll be returning two responses to the client after a comment is submitted. This is why it is preferable to correct the first response.
# http://example.org/foo-bar/%23comment-570630 -> http://example.org/foo-bar/#comment-570630
RewriteCond %{REQUEST_URI} %23comment-d+$
RewriteRule (.+)/%23-comment(d+)$ http://host/$1/#comment-$2 [R=301]
它未经测试,但应该可以工作(我不确定是否转义 \%
因为它在 mod_rewrite 中具有特殊含义).
It's untested, but should work (I'm unsure about escaping \%
as it has special meaning in mod_rewrite).
这篇关于如何用 %23 重写 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!