Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rewrite a URL with %23 in it?

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?

like image 399
janpio Avatar asked Mar 13 '10 13:03

janpio


People also ask

What is URL rewriting example?

URL rewriting is the process of modifying Uniform Resource Locators (URLs) for various purposes. The URL as a “web address” is a string that, when entered into the browser bar field, directs the browser to move to a given site and page.

How do you rewrite a URL in HTML?

To rewrite urls you need to configure the webserver to do that (for example in apache with . htaccess . But if you want to do that without use server configuration, a bad solution exists: make a folder with the name of the url and put the html into that with the name index.


1 Answers

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

like image 192
Tate Johnson Avatar answered Sep 19 '22 14:09

Tate Johnson