Forum Moderators: phranque

Message Too Old, No Replies

double slashes

         

wilderness

2:10 pm on Mar 25, 2012 (gmt 0)

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



could somebody help me modify these to function?

I've SE's chasing malformed links that I typo'd and then later corrected.
The links do NOT exist.

the "directory//" is caught by one of the solution lines abd redirects as it should, however the "//directory" causes a loop and will not redirect.

One solution:
# Externally redirect to remove double slashes
RewriteCond %{REQUEST_URI} ^(/[^/]+/)/+(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^(/)/+(.*)$
RewriteRule ^. http://www.example.com%1%2 [R=301,L]

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

lucy24

11:04 pm on Mar 25, 2012 (gmt 0)

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



the "directory//" is caught by one of the solution lines and redirects as it should, however the "//directory" causes a loop and will not redirect.
...
RewriteCond %{REQUEST_URI} ^(/[^/]+/)/+(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^(/)/+(.*)$
RewriteRule ^. http://www.example.com%1%2 [R=301,L]

Time for a chorus of "I really hate this damned machine" et cetera.

Is this happening in your config file or in htaccess? This has come up recently, and the difference is crucial. In what follows, any captures are shown in red. I hope you are not color-blind or you will have to read very very slowly ;)

If the rewrite is located in .htaccess, then the first works for requests in the form
www.example.com//blahblah//+optionalblahblah

and the second works with
www.example.com///+optionalblahblah

If it is in your config file, then Condition #1 works for
www.example.com/blahblah//+optionalblahblah

while #2 works for
www.example.com//+optionalblahblah

Let's assume for the sake of discussion that you are in the config file. If so, the rule redirects

www.example.com/blahblah//+optionalblahblah
to
www.example.com/blahblah/optionalblahblah

and

www.example.com//+optionalblahblah
to -- pay close attention now --
www.example.com//optionalblahblah

If someone comes along after me and says I've got it exactly backward, he is probably right.

g1smd

11:23 pm on Mar 25, 2012 (gmt 0)

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



I forget if REQUEST_URI differs in htaccess and server config context.

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


In my mind this works. On a real server, in real life... dunno.

wilderness

12:21 am on Mar 26, 2012 (gmt 0)

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



lucy, we've discussed many times that we're both on shared hosting.

g1smd, second line still causes a loop and fails to redirect.

Many thanks to both of you.

g1smd

12:24 am on Mar 26, 2012 (gmt 0)

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



What happens if you swap the order of the two conditions before the single rule?

Maybe this has to be done with two separate rules, each with one condition. Maybe try that too.

I suspect that multiple leading contiguous slashes are suppressed before the requested URI is stored in the REQUEST_URI server variable. You might need to test THE_REQUEST instead.

wilderness

12:38 am on Mar 26, 2012 (gmt 0)

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



g1smd,
These errors are all my fault.
I reactivated 521 pages in sections, not verifying the links until completion of the 521. it took me two days to clean up all the typos.

Now the SE's are chasing the typos's.
I've redirected some 404's hoping the SE's would catch on , however I'm not redirecting hundreds.

I've eliminated the second cond and added left only the first intact, which should slow things down.
-----------
Jim had another two part that looked like this (I didn't save the URL):
(he specified that they had to be in this specific order, however neither of these would redirect for me when in place, despite clearing cache.)

# Externally redirect to remove multiple contiguous slashes at beginning or end of URL
RewriteCond %{REQUEST_URI} ^//+(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^(.*/)/+$
RewriteRule / http://www.example.com/%1 [R=301,L]

# Externally redirect to remove multiple contiguous slashes embedded in URL
RewriteCond %{REQUEST_URI} ^/([^/]+)//+(.*)$
RewriteRule // http://www.example.com/%1/%2 [R=301,L]

lucy24

1:15 am on Mar 26, 2012 (gmt 0)

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



we've discussed many times that we're both on shared hosting

Yes, we have. I have no idea why, but I tend to answer posts without looking at who asked the question except for a quick glance to see if they're a brand spankin newbie ("posts: 1").

Mea culpa.

I suspect that multiple leading contiguous slashes are suppressed before the requested URI is stored in the REQUEST_URI server variable. You might need to test THE_REQUEST instead.

fwiw: I just recently had a request that arrived with leading double-slash (probably cut & paste from a human). All related image requests came with the same double slash, and were processed without complaint.

:: detour for quick test on, ahem, my own shared hosting ::

Interesting. I did this:

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

Of course I would never do this in a "real" Rewrite ;) The non-captured r is to ::cough-cough:: prevent an infinite Redirect (caught by the browser, not the server).

If I request
www.example.com/foobar.html

I get redirected to
www.example.com//fooba

If I request
www.example.com//directory//foobar.html

I get redirected to
www.example.com///directory//fooba

and so on. In other words, the leading slash in the REQUEST_URI is captured and added to the redirect. If you start with a bunch of slashes, the output will have one more.

But wait!
If I change one tiny thing in the Rule, eliminating the slash so it says only
RewriteRule foobar http://www.example.com%1 [R=301,L]
everything changes.

Now, if I request
www.example.com/foobar.html

I get redirected to
www.example.com/fooba
as you would expect based on the previous version.

But if I request
www.example.com///foobar.html

I still get redirected to
www.example.com/fooba

And if I request
www.example.com//directory//foobar.html
--again, with any number of leading slashes--

I get redirected to
www.example.com/directory//fooba

Huh what?

wilderness

2:55 am on Mar 26, 2012 (gmt 0)

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



FWIW and primarily for the benefit of others.

The lines that both I and g1smd provided, may in fact function as intended.

My most sincere apologies.

I had another redirect in place that was preventing the page I was using for testing these particular lines from functioning.

g1smd

7:20 am on Mar 26, 2012 (gmt 0)

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



No problem. I often chase a bug in one piece of code only to find the problem is a knock-on effect from something else further back or in another file.

lucy24

10:07 pm on Mar 26, 2012 (gmt 0)

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



As long as you're here, can you shed any light on the, er, unexpected behavior I encountered above? Obviously something other than the Rewrite itself is making the slashes behave differently in the two versions of the rule.

g1smd

10:48 pm on Mar 26, 2012 (gmt 0)

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



I don't know. Without setting the code up and testing it for myself I can't really say. Jim would have known.
It could be an "undocumented feature" of course.