homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

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


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)

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)


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



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

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]



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

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)

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)

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)

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]



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

That's very useful indeed. Thanks again.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved