Intricate rewrite needed

Discussion in 'Apache' started by MISTERRMAC, Feb 23, 2007.

  1. #1
    I have a situation where I need to redirect a site to https unless in one of a few folders (there are roughly 200 folders in the root but only 3 or four count in this case).

    I also need to make sure the domain starts with www. and is all lowercase.

    I'm no .htaccess pro but I can get around with it a bit but so far, what I have come up with has not worked correctly.

    
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{SERVER_PORT} !^443$
    RewriteCond %{HTTP_HOST} !(^www\.domain\.com|/directory/|/folder/|/twolevel/folder/)+
    RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
    
    Code (markup):
    the regex is correct and matches the strings when tested in other applications but the only time it works here is when the www is left off the domain name domain.

    [EDIT]Not in the above - if there are any GET or POST values, they need to be passed through the redirect as well.

    Hopefully someone can come up with something without needing a mile of code. :)

    --
    Loren
     
    MISTERRMAC, Feb 23, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It's your RewriteCond with the HTTP_HOST that is wrong - you have it set so that the rewriterule only applies if the host does not start with www.domain.com. From your description it sounds like you wanted it the other way round.

    The HTTP_HOST value will only contain the hostname (i.e. localhost, domain.com, www.domain.com, sub.domain.com) so to match the directory as you've tried you'd need another rewritecond against REQUEST_URI or similar.

    GET values will be preserved since they are passed through the URI, which your rewrite rules don't change (other than to use https). The server will return a permanently moved response that tells the browser to look for the requested page in the new location. It's up to the browser to resend any POST data for the new request, and I don't know what will happen there.

    I would try something like:
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{SERVER_PORT} !^443$
    RewriteCond %{HTTP_HOST} ^www.domain.com$
    RewriteCond %{REQUEST_URI} !^/(folders|that|dont|goto|https)
    RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
    Code (markup):
     
    rodney88, Feb 25, 2007 IP
  3. MISTERRMAC

    MISTERRMAC Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    AH-HA HTTP_HOST I was misunderstanding to contain the full URL minus any GET parameters, which is wrong.

    As for the domain name, I guess I wasn't quite clear. One rewrite condition should be if the domain name is incorrect but that really only applies if in https - to avoid any possible SSL notice.

    One thing I didn't mention or try to include is that the folders rule needs to drop us out of https if in one of the folders.

    So, here's all the rules that need to be applied

    1. If https and in a special folder --> redirect to http
    2. If not https and not in a special folder --> redirect to https
    3. If https and domain not www.domain.com --> redirect to correct domain

    Part of my confusion was probably in trying to combine 2 and 3 into one rewrite.

    So, here's what I'll try:
    
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{SERVER_PORT} ^443$
    RewriteCond %{REQUEST_URI} ^/(folders|that|dont|goto|https)+
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
    RewriteCond %{SERVER_PORT} !^443$
    RewriteCond %{REQUEST_URI} !^/(folders|that|dont|goto|https)+
    RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301]
    RewriteCond %{SERVER_PORT} ^443$
    RewriteCond %{HTTP_HOST} !^www.domain.com$
    RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
    
    Code (markup):
    I notice the + (which means 'one or more of the preceding group') was removed from the second RewriteCond line. Was that just a typo or is it really not needed here?

    Finally, the POST values are not being resubmitted. One attempt I made was close to working but was causing issues because of missing post values.

    Hopefully that will work - I'll respond back after some testing.

    --
    Loren
     
    MISTERRMAC, Feb 25, 2007 IP
  4. MISTERRMAC

    MISTERRMAC Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No-Go... The redirect when within a special folder back to http ends up failing and actually causes a 401 error. I have checked the spelling multiple times. It also appears that a redirect is still going on even when in https and not in a special folder - the POST values are still begin lost.

    --
    Loren
     
    MISTERRMAC, Feb 25, 2007 IP