views:

50

answers:

1

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?

A: 

%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).

Tate Johnson
Problem is that the response is fine on all systems I could test it. It's # in the URL and the redirect works, I don't know where and why this could break.
Jan P.
To your rewrite rule: Doesn't work. The matching of the request_uri doesn't seem to work, that's where I got stuck and come here to ask, too.
Jan P.
`RewriteCond` isn't being satisfied? Have you tried escaping the percentage character?
Tate Johnson
Yes. first thing I did. I also tried to refine the RewriteRule with this online tester http://civilolydnad.se/projects/rewriterule/ and made it to `(.+)\/\%23comment-(.+)$ http://host/$1/#comment-$2 [R=301]`, but this also doesn't work on my server. Quite strange.
Jan P.