如何用%23重写URL?

jan*_*pio 5 wordpress .htaccess mod-rewrite

我有一个(wordpress)博客,在评论后,用户被重定向回页面,并带有一个锚点给他们的评论.应该是这样的:

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

但不知何故,我得到了很多404用于此类URL的日志文件:

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

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

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

Tat*_*son 5

%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]
Run Code Online (Sandbox Code Playgroud)

它未经测试,但应该可以工作(我不确定是否要转义,\%因为它在 mod_rewrite 中具有特殊含义)。