如何重写包含 %23 的 URL?

发布于 2024-08-25 04:38:08 字数 440 浏览 6 评论 0原文

我有一个(wordpress)博客,在发表评论后,用户将被重定向回带有评论锚点的页面。应如下所示:

http://example.org/foo-bar/#comment-570630< /a>

但不知何故,我在此类 URL 的日志文件中收到了很多 404:

http:// example.org/foo-bar/%23comment-570630

有没有办法编写 .htaccess 重写规则来解决这个问题?

奖金问题: 知道为什么会发生这种情况以及我能做些什么吗?

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

But somehow I get a lot of 404 ins my logfiles for such URLs:

http://example.org/foo-bar/%23comment-570630

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沐歌 2024-09-01 04:38:08

%23# 的 URL 编码表示形式。我怀疑你的重写规则不满足%23。您应该调查响应是如何构建的。具体来说,任何 URL 编码函数。

但是,可以通过重写规则来解决您的问题。请注意,提交评论后您将向客户返回两个回复。这就是为什么最好纠正第一个响应。

# 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 中具有特殊含义)。

%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]

It's untested, but should work (I'm unsure about escaping \% as it has special meaning in mod_rewrite).

浴红衣 2024-09-01 04:38:08

您是否尝试过 B 标志

RewriteCond %{REQUEST_URI} %23comment-\d+$
RewriteRule (.+)\/%23-comment(\d+)$ http://host/$1/#comment-$2 [B,R=301]

未经您的具体案例测试,但用于相关问题。

Have you tried the B Flag?

RewriteCond %{REQUEST_URI} %23comment-\d+$
RewriteRule (.+)\/%23-comment(\d+)$ http://host/$1/#comment-$2 [B,R=301]

untested with your specific case, but used for a related problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文