Forum Moderators: phranque

Message Too Old, No Replies

Rewritecond to remove slashes at the end

         

thosecars82

6:57 pm on Dec 31, 2009 (gmt 0)

10+ Year Member



Hello

If I write

RewriteCond %{REQUEST_URI} ^/a$[NC]
RewriteRule ^(.*)$ http://www.example.com/ [L,R=301,NC]

then it removes the character "a" from http://www.example.com/a and it leaves the url as http://www.example.com. So far, so good.

But if you want to do the same with the slash instead of the character a then it does not work. Why is not working this next code?

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

If http://www.example.com// is browsed, the second slash is not removed, why?
Thanks

g1smd

8:19 pm on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's because you are redirecting to
http://www.example.com[b]//[/b]
again.

jdMorgan

9:05 pm on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't need a RewriteCond for the simple version of this rule. Try just:

# 301 redirect to remove multiple consecutive slashes in the middle or at the end of requested URL-path
RewriteRule ^(.+)//+(.*)$ http://www.example.com/$1/$2 [R=301,L]
# 301 redirect to remove multiple consecutive slashes at beginning of requested URL-path
RewriteRule ^/+(.*)$ http://www.example.com/$1 [R=301,L]

Note that this code is intended for use in www.example.com/.htaccess where the requested URL-path as seen by RewriteRule will already be stripped of one leading slash.

If this doesn't work, there's a more sophisticated version posted here that I can go dig up if you can't find it by searching.

Jim

thosecars82

11:08 am on Jan 1, 2010 (gmt 0)

10+ Year Member



Thanks
It does not work and I checked that I had changed the ¦ symbol as you had mentioned.
For example I browse
http://www.example.com///
and it does not remove the not needed slashes.
Any idea?
Thanks

jdMorgan

6:56 pm on Jan 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bit short on time today -- Try this search [google.com] for previous solutions.

Jim