Forum Moderators: phranque

Message Too Old, No Replies

Double slash after .com redirect?

Working for .com//file//, not .com//file/

         

LunaC

8:10 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



I'm currently using this and it works perfectly for many double slash problems but not for .com// (unless the request is .com//file//)


# remove multiple slashes anywhere in url (not working for .com//)
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . http://www.example.com%1/%2 [R=301,L]

It does 301 redirect these to example.com/test/:
example.com/test//
example.com//test//

but not these (returns a 200 and shows no change in the URL)
example.com//test/
example.com//

As far as I can see it works only if there's another double slash elsewhere in the url, but not if the only double slash is directly after the .com/

I've tried a few variations and these are the results. (Browser cache was cleared and restarted each time.)

No redirects happened even for .com//file// Header response is 200 OK:


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


Properly 301s .com//file// to .com/file/ but not .com//file/ (200 OK), same as the original code.

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

Can anyone spot the problem?

jdMorgan

5:48 am on Feb 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're fighting Apache here -- It does some "silent" internal clean-up on %{REQUEST_URI} and on the URL-path 'seen' by RewriteRule. It also un-encodes any encoded characters in the request before creating these variables.

So we're back to looking at the actual client HTTP request header. Here's one of many ways to code it:


# Redirect to remove multiple slashes within URL-path
RewriteRule ^(([^/]+/)*)/+(.*)$ http://www.example.com/$1$3 [R=301,L]
#
# Redirect to remove multiple slashes before URL-path
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ //+([^\ ]*)
RewriteRule .* http://www.example.com/%1 [R=301,L]

For reference, THE_REQUEST is the actual request header received from the client, and might look like this:
GET //scripts/quux.jsp?few=bars&king=kong HTTP/1.1

The rules should be used in the order shown -- As you observed, you get a "free clean-up" of leading slashes from the first rule if there are also double slashes within the URL-path.

I also built these so that they fix "more-than-one" slashes, not just double-slashes.

Not tested -- but this should give you the idea, anyway...

Jim

LunaC

6:37 pm on Feb 27, 2008 (gmt 0)

10+ Year Member



Ah, OK thanks. I couldn't get the first rule working on my server (nothing happened at all, 200), the second is the missing puzzle piece I'd been searching for.

I ended up using a combo of the first rule I had been using and adding your second rule after it. It now removes the .com// and the file//.

It does do multiple 301s if theres a ton of slashes in the file path, but I can live with that while I try to learn how to read what that says exactly, and how to edit this a bit to get the first rule you wrote working on my server.

Thanks again for the code and the explanation. :)

jdMorgan

7:41 pm on Feb 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually I just tested, and had success with this rule in /.htaccess:

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

The pattern-analysis might make your head hurt a bit -- it did mine, but it covers all the cases I tested.

It redirected all these cases:
example.com//images//logo.gif
example.com//images/logo.gif
example.com/images//logo.gif
example.com//favicon.ico

as well as others with more directory-levels.

Jim

LunaC

6:52 pm on Feb 29, 2008 (gmt 0)

10+ Year Member



Hehe, Headache.. no, for that I'd have to *understand* all/most/some of it... A severe case of vertigo while trying though, Oh yeah!

There's something on my server that's blocking that from working (slashes left intact, 200). I tried it on another site on a separate server and it worked like a charm. Smooth, single 301!

I'll contact my host today to see what it could be.

Thanks yet again :)

jdMorgan

4:24 am on Mar 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check for:

mod_speling CheckSpelling on
mod_negotiation enabled using Options +MultiViews
AcceptPathInfo on

All these can defeat mod_rewrites by "fixing-up" internal paths before mod_rewrite receives the URL.

Since the working code used THE_REQUEST (which is what the client originally sent regardless of intervening server-internal URLs rewrites/changes) it would have been unaffected by these. But my single RewriteRule *would* be affected by these.

Jim

sc0ttkclark

8:11 pm on Mar 14, 2008 (gmt 0)

10+ Year Member



What about a url like

http://www.examplewordpressblog.com/page//page/page/

It won't remove it from within it.

Also, found out that

http://www.examplewordpressblog.com/page/page/page//

doesn't get fixed either.

We're working with ISAPI Rewrite v3 with htacess files. Your code works correctly for everything else. Just looks like it's fixing only the start.

Here's the code I'm using:

RewriteRule ^(([^/]+/)*)/+(.*)$ /$1$3 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ //+([^\ ]*)
RewriteRule .* /%1 [R=301,L]

jdMorgan

3:44 am on Mar 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The syntax and rules work a bit differently in ISAPI Rewrite versus Apache mod_rewrite, and I "don't do MS servers," so I'm sorry but I can't answer your question. It may have to do with slight differences in the local URL-path 'seen' by RewriteRule, or perhaps with regex operator precedence or 'greediness' differences.

As stated, the short-version code I posted works for the listed examples when installed in .htaccess on an Apache server; No warranty or claim of suitability for any other application, I'm afraid. :)

Jim