MarkOly - 6:44 pm on Jul 27, 2013 (gmt 0)
This one is really close:
# Redirect URL containing valid characters to remove trailing invalid characters
RewriteRule ^([\w/-]+(\.\w+)?)?[^a-zA-Z\d].* http://www.example.com/$1 [R=301,L]
It redirects the trailing character requests with a 301. Works for trailing periods. Only problem is that valid page requests redirect to themselves.
I've been monkeying around with this in the meantime myself. This would all be really easy if I didn't care about redirecting trailing periods. #10 seems to do everything I want, except for trailing periods. So I thought about it. By the time it gets to this rule, the only valid requests containing a period are going to be images, css, etc. So if I were to rule out those things, then I could modify #10 to also work for periods (by removing the two periods from the pattern):
# Redirect URL containing valid characters to remove trailing invalid characters except for specific file types and folders
RewriteCond $1 !\.(css|gif|jpe?g|bmp|png|js|ico|xml|txt)$ [NC]
RewriteCond $1 !^(shopping-cart-folder|site-stats-folder)/
RewriteRule ^([/0-9a-z_\-]*)[^/0-9a-z_\-]+$ http://www.example.com/$1 [NC,R=301,L]
It does work - and for multiple trailing chars after I got daring and added the + before the $ in the pattern. But am I skating on thin ice here? When I first tested the above RewriteRule without the RewriteCond ruling out css, etc, things went haywire. So this seems like it could be really dangerous. I haven't tested it extensively. But so far so good. The only issue I found so far is that something like example.com/bogus. gets a 301 first to example/bogus then example/bogus gets its 404.