Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite help

         

ApachePatrick

5:14 am on May 2, 2009 (gmt 0)

10+ Year Member



Hi everyone,

I've been searching the Internet all night and I keep coming back to this board so i thought I should post since everything I am trying is not working.

Ok, i have a Windows (yes, Windows) box running Apache 2.2 and the server is SSL protected. For the purposes of my explaination let's call my domain www.example.com

My main doc root is defined in httpd.conf as

DocumentRoot "C:/Apache/htdocs"

I have a folder in this directory, let us call it "test" so

"C:/Apache/htdocs/test"

I want to make it so that requests for pages in this test folder are NOT served over https, in fact, I want it so that requests for [example...] are redirected to http://www.example.com/test/page.php

I have mod_rewrite going on like this

# rewrite environment
RewriteEngine on
RewriteLog C:\Apache\logs\https_rewrite_log
RewriteLogLevel 9

# non-www to www
RewriteCond %{HTTP_HOST} ^example\.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#tested and confirmed working on 5/1/2009

# Secure sections/pages..everything else
RewriteCond %{SERVER_PORT} ^80$
RewriteRule (.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#tested and confirmed working on 5/1/2009

..so when I add something like this thinking it will work, it does not :-(

# https --> http for test section
RewriteCond %{SERVER_PORT} !^80$
RewriteCond %{REQUEST_URI} ^/test(/.*)$
RewriteRule (.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Can anyone spot my issue? Is it with the regex? I have tried to

1) put this at the top of my httpd.conf

2) in a .htaccess file in the test directory (for that I commented this part, still no dice..)

RewriteCond %{REQUEST_URI} ^/test(/.*)$

3) in a <Directory> container for the test directory in my httpd.conf file

Nothing seems to work. Currently it looks like I am on the right track with an infinite loop... when I ask for [example.com...] it redirects to the http://www.example.com/test/file.php ( i think, based on rewrite logs) but then it picks up on my "site wide" rewrite rule for everything that is hitting port 80 to be redirected to 443 and thus goes back to https and on and on....

Any one know how to get around this?

I NEED to have the test directory content do a real redirect (HTTP 302) from http to https for what i am trying to ultimately test out once this gets working.

Thanks in advance for any pointers!

- ApachePatrick

[edited by: jdMorgan at 5:12 pm (utc) on May 2, 2009]
[edit reason] example.com [/edit]

jdMorgan

2:39 pm on May 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The rules were not in correct order, and the http->https redirect code failed to exclude "/test", thus leading to a loop:
 
# Redirect requests for all resources except "/test" to HTTPS
RewriteCond %{SERVER_PORT} !^443$
RewriteCond $1 !^test/?
RewriteRule ^/?(.*)$ https://www.example.com/$1 [R=301,L]
#
# Redirect requests for /test subdirectory to HTTP
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^/?test(/.*)?$ http://www.example.com/test$1 [R=301,L]
#
# Redirect requests for non-www to canonical www subdomain
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^/?(.*)$ http://www.example.com/$1 [R=301,L]

The leading "/?" on the RewriteRule patterns is used to make this code "portable" between .htaccess or <Directory> sections in config files to global server config sections. It simply makes the leading slash optional. You can delete it if the code goes into .htaccess or a <Directory> section, but the leading slash will be required if the code is placed in a <VirtualHost> container outside of any <Directory> container.

I changed the port detection logic to look for "NOT 443" instead of "80" and vice-versa. This is a little more robust, and allows for HTTP operation on additional or alternate ports such as 8080. (Basically, 443 is HTTPS, and you can use almost any other ports you like for HTTP. This is often done to deploy back-end servers, etc. Consider this to be simply inexpensive "future-proofing.")

Your domain canonicalization rule could be made more robust:


RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteRule ^/?(.*)$ http%2://www.example.com/$1 [R=301,L]

This modification redirects request for any hostname other than *exactly* www.example.com, and so takes care of casing errors, FQDN hostname requests, and appended port numbers. The rule also preserves the requested HTTP or HTTPS protocol.

Important: Replace all broken pipe "¦" characters here with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

ApachePatrick

4:14 pm on May 2, 2009 (gmt 0)

10+ Year Member



Thank you for this! This is exactly what I needed.