Forum Discussion

adamo_1516's avatar
adamo_1516
Icon for Nimbostratus rankNimbostratus
Aug 14, 2007

exception logic for selective https redirect

yes, i'm losing my mind.

 

 

i've got a rule we put in place to take specific subdomains and statically redirect the URL's to https. it works great, but we're now seeing we need to add exception logic for a specific URI beforehand, and i've been unable to figure it out.

 

 

our existing rule looks like this:

 

 

when HTTP_REQUEST {

 

if { ([HTTP::host] starts_with "subdomain1." ) or ([HTTP::host] starts_with "subdomain2.") } {

 

HTTP::redirect https://[HTTP::host][HTTP::uri]

 

}

 

}

 

 

we need to add logic for this:

 

 

if { ([HTTP::uri] contains "SpecialUrl") } {

 

 

then it needs to allow HTTP, and *NOT* redirect to HTTPS. i'm thinking an 'else' would do it, but i'm not so sure what to do for the statement for the action.. would an HTTP redirect to the same exact URL work (simiular to this):

 

 

HTTP::redirect http://[HTTP::host][HTTP::uri]

 

 

 

i just can't get any of this to tie together, and really need to get this working since our prod environment now has an issue. any help appreciated!!

 

 

-adam

4 Replies

  • If you want to redirect if the host starts with sub1 or sub2, but not if the URI contains the special string, you can use 'not':

    
    when HTTP_REQUEST {
       if { ([HTTP::host] starts_with "subdomain1." or [HTTP::host] starts_with "subdomain2.") and (not ([HTTP::uri] contains "SpecialUrl"))} {
          HTTP::redirect https://[HTTP::host][HTTP::uri]
       }
    }

    Aaron
  • awesome! i'm going to give this a try tonight.

     

     

    another quickie: off the top of your head, is there any good way to have a list domains instead of constantly adding 'or' statements and just have the original logic apply to those? like an array or something. it seems it'd be more efficient in the long run, as we'll likely add a good bit more domains to this one day.

     

     

    eventually, we'll negate it to allow only certain subdomains http, and force https to all.

     

     

    -adam
  • How about:

     

     

    if { not [HTTP::uri] contains "SpecialUrl" and (([HTTP::host] starts_with "subdomain1." ) or ([HTTP::host] starts_with "subdomain2.")) } {

     

    HTTP::redirect https://[HTTP::host][HTTP::uri]

     

    }

     

    }
  • First, I saw a more eloquent way to parse the subdomain from the getfield wiki page (Click here)"

    [getfield [string tolower [HTTP::host]] "example.com" 1]

    This would set the host header value to lower case and then split the value on the literal string "example.com" and return the 1st field (the subdomain).

    You can use a class (called a datagroup in the GUI) which contains the subdomains you want to redirect. You can create a datagroup under Local Traffic >> iRules >> Datagroups.

    You can then use matchclass (Click here) to check if the requested host starts with one of the subdomains in the class.

    
    class redirect_hosts {
       subdomain1.
       subdomain2.
       subdomain3.
    }

    
    when HTTP_REQUEST {
       if {([matchclass [getfield [string tolower [HTTP::host]] "example.com" 1]] equals $::redirect_hosts) and (not ([HTTP::uri] contains "SpecialUrl"))} {
          HTTP::redirect https://[HTTP::host][HTTP::uri]
       }
    }

    The class approach is cleaner as far as the rule goes. But it might be slightly faster to use an if and then a switch statement:

    
    if {not ([HTTP::path] contains "SpecialUrl")}{
        use getfield to split the host header value and grab anything that comes before "example.com"
        set that remaining string to lower case
       switch [getfield [string tolower [HTTP::host]] "example.com" 1] {
          subdomain1. -
          subdomain2. -
          subdomain3. {
              redirect matching requests to https
             HTTP::redirect https://[HTTP::host][HTTP::uri]
          }
          default {
              do nothing; let request be sent to pool per VIP configuration
          }
       }
    }

    [edited to fix the order of string tolower and getfield]

    Aaron