Forum Discussion

JonasD_46068's avatar
JonasD_46068
Icon for Nimbostratus rankNimbostratus
Mar 07, 2007

iRule for HTTPS & www

I have 2 iRules:

 

 

when HTTP_REQUEST { HTTP::redirect https://[HTTP::host][HTTP::uri] }

 

 

and

 

 

when HTTP_REQUEST { if {[HTTP::host] equals "testdomain.net"} {HTTP::redirect "https://www.testdomain.net[HTTP::uri]" } }

 

 

When both rules are applied they obviously cancel each other out. When I only have the 2nd rule applied obviously the redirection doesn't work. What I am trying to do is 2 things in 1 irule (if it needs to be more than 1 rule that is fine too). I want to redirect connections that go to http to https. I also want to redirect requests to testdomain.net without the www to https also. How can I do this?
  • This should do it nicely (if a little unelegantly):

    when HTTP_REQUEST {
      set userhost [HTTP::host]
      if { $userhost == "testdomain.net" } {
        set userhost "www.[HTTP::host]"
      }
      HTTP::redirect https://$userhost[HTTP::uri]
    }
  • Thanks!

     

     

    When I type that in I get the following error:

     

     

    01070151:3: Rule [http_no_WWW_redirect] error:

     

    line 1: [wrong args] [set userhost [HTTP::host] if { $userhost == "testdomain.net" } {set userhost "www.[HTTP::host]" } HTTP::redirect https://$userhost[HTTP::uri] ]

     

     

    userhost (the first one is underlined indicating an error)

     

     

     

    Jhd
  • Here's the original rule I typed in that you gave me:

     

     

    when HTTP_REQUEST { set userhost [HTTP::host] if { $userhost == "advisorfolio.net" } {set userhost "www.[HTTP::host]" } HTTP::redirect https://$userhost[HTTP::uri] }
  • I actually implemented this rule and it worked like a champ. I cut and pasted it right from the text above and changed only the doamin name.The syntax errors are something that always drives me nuts so I will paste it in as I have it:

     

     

    when HTTP_REQUEST {

     

    set userhost [HTTP::host]

     

    if { $userhost == "domain.com" } {

     

    set userhost "www.[HTTP::host]"

     

    }

     

    HTTP::redirect https://$userhost[HTTP::uri]

     

    }
  • You replied before I could. You are CORRECT, it was all syntax...i typed it in EXACTLY as you had it line breaks and all and it worked PERFECTLY. Thanks. I do a lot of work with F5s now and these iRules are fantastic.

     

     

    Jhd