Forum Discussion

kraigk_52257's avatar
kraigk_52257
Icon for Nimbostratus rankNimbostratus
Jul 01, 2015

Inefficient iRule, works but not perfect

We have a working iRule that I'm sure can be improved/. The first section is simple redirects from domain.org URL to equivalent https domain.com URL. We discovered that if users browsed directly to redirected URL (http://team.domain.com for example) they got nothing so we added the second section to address that. This works but with a few issues:

It works still but isn't too pretty. Any help would be appreciated by a beginner..

when HTTP_REQUEST {
 if { [HTTP::host] equals "team.domain.org" } {
       HTTP::redirect "https://team.domain.com[HTTP::uri]"}
 if { [HTTP::host] equals "site1.domain.org" } {
       HTTP::redirect "https://site1.domain.com[HTTP::uri]"}
 if { [HTTP::host] equals "site2.domain.org" } {
       HTTP::redirect "https://site2.domain.com[HTTP::uri]"}
 if { [HTTP::host] equals "myteam.domain.org" } {
       HTTP::redirect "https://myteam.domain.com[HTTP::uri]"}

 if { [HTTP::host] equals "team.domain.com" } {
       HTTP::redirect "https://team.domain.com/default.aspx[HTTP::uri]"}
 if { [HTTP::host] equals "site1.domain.com" } {
       HTTP::redirect "https://site1.domain.com/default.aspx[HTTP::uri]"}
 if { [HTTP::host] equals "site2.domain.com" } {
       HTTP::redirect "https://site2.domain.com/default.aspx[HTTP::uri]"}
 if { [HTTP::host] equals "myteam.domain.com" } {
       HTTP::redirect "https://myteam.domain.com/default.aspx[HTTP::uri]"}

}

12 Replies

  • Use the switch statement instead of if and use variable if possible...

    when HTTP_REQUEST {
        switch -glob [string tolower [HTTP::host]] {
          "team.domain.org" { HTTP::redirect "https://team.domain.com[HTTP::uri]"}
          "site1.domain.org" { HTTP::redirect "https://site1.domain.com[HTTP::uri]"}
          "site2.domain.org" { HTTP::redirect "https://site2.domain.com[HTTP::uri]"}
          "myteam.domain.org" { HTTP::redirect "https://myteam.domain.com[HTTP::uri]"}
          "site1.domain.com" -
          "site2.domain.com" -
          "team.domain.com" -
          "myteam.domain.com" { https://[HTTP::host]/default.aspx[HTTP::uri]"}
          }
     }
    
  • nathe's avatar
    nathe
    Icon for Cirrocumulus rankCirrocumulus

    is this neater and more efficient? let me know if it doesn't meet all your requirements. Away from the lab at the moment to test.

    when HTTP_REQUEST {
      switch -glob [string tolower [HTTP::host]] {
       "team.domain.org" { HTTP::respond 301 Location "https://team.domain.com[HTTP::uri]" }
       "site1.domain.org" { HTTP::respond 301 Location "https://site1.domain.com[HTTP::uri]" }
       "site2.domain.org" { HTTP::respond 301 Location "https://site2.domain.com[HTTP::uri]" }
       "myteam.domain.org" { HTTP::respond 301 Location "https://myteam.domain.com[HTTP::uri]" }
       "*.domain.com" { HTTP::respond 301 Location "https://[HTTP::host]/default.aspx" }
     }
    } 
    

    Obviously a 301 is permanent redirect, i've assumed this.

    N

  • I did not see the loop of default.aspx was cause by : /default.aspx[HTTP::uri] the redirect must be :HTTP::redirect "https://myteam.domain.com/default.aspx
  • Nathan,

     

    Thanks - I'm going to test this today and will get back to you. Much cleaner than my mess.

     

    • nathe's avatar
      nathe
      Icon for Cirrocumulus rankCirrocumulus
      No probs. Be good to hear your feedback
  • What will be the effect if the Host: request header matches neither of the strings? I.e., always write explicit code for the 'default' case...

     

    Just my $0.02 worth :-)

     

    -Frank

     

  • Nathan, I did finally get to test and it works well. We added short names as well as they were hitting the vs and failing. Not sure if there is a better way to incorporate them into this. Thanks for your suggestions.

    when HTTP_REQUEST {
      switch -glob [string tolower [HTTP::host]] {
       "team" { HTTP::respond 301 Location "https://team.domain.com[HTTP::uri]" }
       "site1" { HTTP::respond 301 Location "https://site1.domain.com[HTTP::uri]" }
       "site2" { HTTP::respond 301 Location "https://site2.domain.com[HTTP::uri]" }
       "myteam" { HTTP::respond 301 Location "https://myteam.domain.com[HTTP::uri]" }
       "team.domain.org" { HTTP::respond 301 Location "https://team.domain.com[HTTP::uri]" }
       "site1.domain.org" { HTTP::respond 301 Location "https://site1.domain.com[HTTP::uri]" }
       "site2.domain.org" { HTTP::respond 301 Location "https://site2.domain.com[HTTP::uri]" }
       "myteam.domain.org" { HTTP::respond 301 Location "https://myteam.domain.com[HTTP::uri]" }
       "*.domain.com" { HTTP::respond 301 Location "https://[HTTP::host]/default.aspx" }
     }
    } 
    
  • nathe's avatar
    nathe
    Icon for Cirrocumulus rankCirrocumulus

    Could you use a wildcard?

    when HTTP_REQUEST {
      switch -glob [string tolower [HTTP::host]] {
       "team*" { HTTP::respond 301 Location "https://team.domain.com[HTTP::uri]" }
       "site1*" { HTTP::respond 301 Location "https://site1.domain.com[HTTP::uri]" }
       "site2*" { HTTP::respond 301 Location "https://site2.domain.com[HTTP::uri]" }
       "myteam*" { HTTP::respond 301 Location "https://myteam.domain.com[HTTP::uri]" }
       "*.domain.com" { HTTP::respond 301 Location "https://[HTTP::host]/default.aspx" }
     }
    } 
    
    • nathe's avatar
      nathe
      Icon for Cirrocumulus rankCirrocumulus
      Good luck. Mark up if it works. Ta
  • Nathan, I tested and implemented exactly what you suggested and it works perfectly. I really appreciate your help.

        when HTTP_REQUEST {
      switch -glob [string tolower [HTTP::host]] {
       "team*" { HTTP::respond 301 Location "https://team.domain.com[HTTP::uri]" }
       "site1*" { HTTP::respond 301 Location "https://site1.domain.com[HTTP::uri]" }
       "site2*" { HTTP::respond 301 Location "https://site2.domain.com[HTTP::uri]" }
       "myteam*" { HTTP::respond 301 Location "https://myteam.domain.com[HTTP::uri]" }
       "*.domain.com" { HTTP::respond 301 Location "https://[HTTP::host]/default.aspx" }
     }
    }