Forum Discussion

Todd_94419's avatar
Todd_94419
Icon for Nimbostratus rankNimbostratus
Sep 13, 2011

HTTP to HTTPS Redirect based on URI

It would appear that there have been several posts pretaining to this type of question but I have been unable to find something that fits exactly, and I am not able to get it to work.

 

 

Trying to redirect from my HTTP VIP to HTTPS VIP only if certain link is clicked.

 

 

URI for the link is www.company.com/abc

 

 

At that point I would want the link to be redirected to the HTTPS version of that page. This is what I have tried with no luck.

 

 

when HTTP_REQUEST {

 

if {[HTTP::uri] eq "/abc"} {

 

HTTP::redirect "https://www.company.com/abc[HTTP::uri]"

 

}

 

}

 

 

I've tried a 301 redirect as well thinking that might work...

 

 

As you can tell I'm pretty new to F5 and iRules. Sorry if I've overcomplicated things... any help is appreciated.

 

 

Thanks

4 Replies

  • You could do this:

    
    when HTTP_REQUEST {
        if { [HTTP::uri] eq "/abc"}{
            HTTP::redirect "https://[HTTP::host][HTTP::uri]"
        }
    }
    
  • I would suggest changing your comparison from equals to starts_with or contains.

     

     

    If you use equal then the comparison is absolute, so http://www.website.com/abc/foo.html would not qualify in your comparison because of the addition of the "/foo.html".

     

     

    If you used starts_with or contains then the iRule would work.

     

     

    You also might want to string tolower your [HTTP::uri].

     

     

    Change:

     

    if { [HTTP::uri] eq "/abc"}{

     

     

    To:

     

    if { [string tolower [HTTP::uri]] starts_with "/abc"}{

     

     

    Hope this helps.
  • This worked! Thanks everyone, I appreciate the help.

     

     

    I used the [string tolower [HTTP::uri]] as Michael recommended... could you clarify exactly what that does differently then the [string tolower [HTTP::uri]] just in case something comes up, I know what to look for.

     

     

    Thanks again.

     

     

    Sorry, meant to ask the difference between [HTTP::uri] and [string tolower [HTTP::uri]]
  • The HTTP::uri is case sensitive depending on which operating system you are using (Apache vs. IIS, and probably a few others).

     

     

    The [string tolower [HTTP::uri]] forces all of the characters to lower case so that you have a "known state" to compare against.

     

     

    So if the incoming URI is /ABC it forces it to /abc. Doing so makes a comparison like [string tolower [HTTP::uri]] starts_with "/abc" for for both.

     

     

    You can read more about it on the Wiki Entry: http://devcentral.f5.com/wiki/iRules.SetUriToLowerCase.ashx

     

     

    Let me know if you have any more questions. Glad it all worked out for you.