Welcome to WebmasterWorld Guest from 54.158.36.59

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

.htaccess Replace trailing comma with slash from a url

.htaccess Replace trailing comma with slash from a url

     
10:20 am on Dec 1, 2009 (gmt 0)

5+ Year Member



Hi,

I just want that trailing comma from a url is replaced with a trailing slash and here's what I've written


RewriteCond %{REQUEST_URI} (.)+,$
RewriteRule (.+) http://www.example.com/$1/ [R=301,L]

But it's not working.

Help jdMorgan.

11:29 am on Dec 1, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



In what way does it "not work"?

What is the test URL? What do you get back from the website?

If I ask for

example.com/folder[b]/,[/b]
your rule will likely return
www.example.com/folder[b]/,/[/b]
I see.

You might want to clarify your requirements.

11:47 am on Dec 1, 2009 (gmt 0)

5+ Year Member



Hi,

my wrong url is www.example.com/section1, ending with a comma and which I want to make www.example.com/section1/

Thanks.

12:37 pm on Dec 1, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The RewriteCond pattern is incorrect, and the RewriteCond isn't needed anyway.

The key is to not include the trailing comma in the parenthesized pattern match to be back-referenced by $1 :


RewriteRule ^(.+),$ http://www.example.com/$1/ [R=301,L]

Jim
1:22 pm on Dec 1, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



One clarification.

Do you want to fix just a single trailing comma, or will you ever have other characters (that you also need to dump) appended directly after the comma?

1:32 pm on Dec 1, 2009 (gmt 0)

5+ Year Member



Hi Jim and g1smd,

your solution worked like a charm.

Actually one of my website started getting lots of 404 hits and the incorrect urls have trailing commas (the one I posted), rest of the url is just fine. I've not been able to locate from where this comma is coming yet but meanwhile I thought to remove the error log at least by adding this rule.

Thanks for your help!

@g1smd, yep, this comma was the only issue at the moment but you may please suggest a better rule if you have any ideas. Thanks mate.

3:01 pm on Dec 1, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The comma comes from forum, blog, and CMS-driven sites that use auto-linking for typed-in URLs. A user types a URL as part of a post, using proper English and punctuation. The forum, blog, or CMS software has a bug such that it incorrectly includes the trailing punctuation as if it were a legitimate part of the URL. You can sometimes see where these incoming links come from by using Google WebmasterTools.

My sites usually fix trailing commas, colons, semi-colons, periods, parentheses, and other such punctuation.

5:00 pm on Dec 1, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



An almost infinite variety of rewriterules, depending on your needs...

# Externally redirect to remove all invalid trailing characters (often
# the result of punctuation following links posted in forums & blogs)
RewriteRule ^(.+)[^0-9a-z/]$ http://www.example.com/$1/ [NC,R=301,L]

Jim
6:37 am on Dec 2, 2009 (gmt 0)

5+ Year Member



That's very useful indeed. Thanks again.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month