Forum Moderators: phranque

Message Too Old, No Replies

Excluding a directory from a redirect

https for one folder, http for all others

         

tuatara

6:48 am on Nov 26, 2007 (gmt 0)

10+ Year Member



I have a directory ("support") that should always be accessed over a secure connection. That's simple enough with an .htaccess file; but once someone leaves that directory, we'd like to move them back to plain http ... unless it's not a GET request (probably someone using the search form).

# requests to 'support' should be secure
RewriteCond %{HTTPS} ^off$
RewriteRule ^support(.*) https://%{SERVER_NAME}/support$1 [L,R=301]

# secure requests outside 'support' should revert to http
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{HTTPS} ^on$
# This next line *should* filter out requests to the support directory
RewriteCond %{REQUEST_URI}!^/support
# Have also tried ...
#RewriteCond $1!^/support
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

Unfortunately, secure requests to the 'support' directory are not being filtered out by this line:

RewriteCond %{REQUEST_URI}!^/support

which creates an endless loop. Does anyone have any suggestion what I'm doing wrong?

This site is running a CMS system which has its own redirect after the lines above. I'll include those just in case it could be affecting things.

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

This is on Apache 2.2.3. Thanks for any help!

Matt

jdMorgan

3:19 pm on Nov 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see anything obviously wrong with this code. The exclusion should work as you expect. So this leads me to ask, have you completely flushed your browser cache after making changes to your code?

Be aware that using %{HTTPS} ^on$ doesn't work on all servers, and you might want to try %{SERVER_PORT} ^443$ instead. Also, using %{SERVER_NAME} instead of %{HTTP_HOST} may also be problematic if the server name is not the canonical domain.

Note that in .htaccess, while %{REQUEST_URI} may have the value "/secure", the value of $1 will be "secure" -- Note the difference in the leading slash.

Jim

tuatara

6:46 am on Nov 27, 2007 (gmt 0)

10+ Year Member



Thank you Jim! Yes, I've cleared the cache, and tried it with different browsers just to be sure.

I just tried it now in Safari and it get into the endless loop, but it was also showing each page as it redirected. It went something like this (I'm sorry I need to use example.com -- long story):

[example.com...]
http://example.com/index.php?q=support/page
http://example.com/support/page

And then back to the beginning again. Which makes me wonder if the friendly-urls stuff that is already there, courtesy of the CMS, is messing things up. Shouldn't it stop processing once it gets to the [L] directive?

Thanks
Matt

jdMorgan

3:05 pm on Nov 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Find the code (in your .htaccess files or your script(s) that do this redirect:
http://example.com/index.php?q=support/page --> http://example.com/support/page
and your problem will be almost solved... :)

This function is not part of the code you posted above, but it is apparently what is interfering here.

Jim

tuatara

7:46 pm on Nov 27, 2007 (gmt 0)

10+ Year Member



Thanks Jim. It looks like the last part of code I posted in the first message:

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I'm just not sure how to make them all work happily together ... I tried changing the order but it didn't seem to make a difference.

jdMorgan

8:06 pm on Nov 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That code does not do an external redirect. It does an internal rewrite. Some other code is causing a redirect, and that's what you've got to find.

Jim

jdMorgan

7:58 pm on Dec 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have thought about this for awhile, and have another question: Is /index.php being used to generate pages requested from the /support directory?

If so, then you'll need to add another exclusion in the HTTPS-to-HTTP redirect rule, such as


RewriteCond %{REQUEST_URI}?%{QUERY_STRING} ^/index\.php\?q=support/

Otherwise, since "index.php" is not in the support directory, it will be redirected to HTTP as the rules specify.

Jim