Forum Moderators: phranque

Message Too Old, No Replies

http to https redirect rule leads to (infinite) Redirect Loop error

HTTPS RewriteRule Redirect Loop

         

LawrenceW

9:06 pm on Dec 24, 2008 (gmt 0)

10+ Year Member



Hi,
My goal is to redirect all incoming HTTP requests to a particular part of a larger domain using HTTPS request. Everything else stays the same except HTTPS vs HTTP protocol.

The following rewrite rule redirects successfully to HTTPS (visible in the FireFox URL line).

RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^(.*)/myweb/myniche/(.*)$
RewriteRule ^(.*)$ $1__SEPARATOR__%{SERVER_NAME} [C]
RewriteRule ^(.*)__SEPARATOR__(.*)$ [$2$1...] [L,R=permanent]

However, the HTTPS request then gets stuck in an infinite loop --
Redirect Loop ..
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

(The looping is verified in defaultApacheRewrite.log too.)

The first condition should fail for the redirected HTTPS request and
let it pass, but it does not.

I have also tried
RewriteCond %{SERVER_PORT} !^443$
and
RewriteCond %{SERVER_PORT} ^80$

but none of them works.

Any suggestions/ideas? Thanks in advance.

g1smd

10:10 pm on Dec 24, 2008 (gmt 0)

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



Should you be testing THE_REQUEST instead of REQUEST_URI here?

When a pattern begins ^(.*) or ends (.*)$ and the back-reference isn't going to be re-used, these parts can be completely omitted.

jdMorgan

10:46 pm on Dec 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



%{HTTPS} is not a native server variable, and may not be defined at the time that this rule runs.

Try using "%{SERVER_PORT} !^443$" instead.

Also, I see no reason why the REQUEST_URI RewriteCond and the chained rule are needed -- it's over-complicated. Do it all at once with something like:


RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*/myweb/myniche/.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

This rule could still use some improvement if the first ".*" in the RewriteRule pattern can be eliminated or replaced with a more-specific pattern; Use of multiple ".*" subpatterns in a rule result in the regex matching engine having to make dozens, hundreds, or even thousands of "back-off-and-try-again" matching attempts and are hugely inefficient.

Since you didn't provide example request URL-paths, all I can do is make that general recommendation...

Jim