Forum Moderators: phranque

Message Too Old, No Replies

Rewriting for an accidental parenthesis in URL

         

helmet

6:30 pm on Oct 4, 2009 (gmt 0)

10+ Year Member



I just made a comment on someone's blog where I referred back to my blog, but I put my link in parentheses, and though I usually put spaces at the beginning and end of a link in parentheses, I forgot to this time. So anyone who clicks on that link will get a 404 because there's a ")" on the end:

http://example.com/my-blog-post/)

How can I rewrite this to take out the parenthesis when parentheses are special characters used in regular expressions? Everything I've tried results in a 500 error.

helmet

6:39 pm on Oct 4, 2009 (gmt 0)

10+ Year Member



This solution is working

[webmasterworld.com...]

jdMorgan

7:16 pm on Oct 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The general answer to your question, "How can I rewrite this to take out the parenthesis when parentheses are special characters used in regular expressions?" is to "escape" those special characters.

So, using RedirectMatch or RewriteRule -- both of which use regular expressions, you do something like:


RedirectMatch 301 ^/my-URL-her[b]e\)$[/b] http://www.example.com/my-URL-here

A more general solution could also be used -- one that removes any and all 'special characters' that are not expected to follow a URL but that could get added by faulty forum/blog autolinking code: Characters such as periods, commas, colons, semicolons, quotes, hyphens, exclamations point, etc.

In fact, there are so many characters that *could* occur, that it might be easier to simply redirect any URL that ends with *anything* other than a letter, number, or slash...


RewriteRule ^(.+)[^/0-9a-z]$ http://www.example.com/$1 [NC,R=301,L]

or perhaps a more robust solution, allowing only specified characters in specific parts of the URL-path, and thereby allowing multiple trailing 'special characters' to be removed:

RewriteRule ^([0-9a-z]([/0-9a-z._\-]*[0-9a-z])?)[^/0-9a-z]+$ http://www.example.com/$1 [NC,R=301,L]

Jim

acimag

3:46 am on Oct 5, 2009 (gmt 0)

10+ Year Member



I have been looking for a great way to rewrite many URLS to new urls all at once. Great help!