Forum Discussion

Khoa_Le_50892's avatar
Khoa_Le_50892
Icon for Nimbostratus rankNimbostratus
Feb 23, 2011

iRules - multiple matches

Hi all,

 

 

I'm new to iRules so need your help on fine tune of my iRule below.

 

 

 

We are having serveral domain and subdomain and forward to same VS on F5. F5 will need to examine the HTTP REQUEST and redirect the request to correct location:

 

when HTTP_REQUEST {

 

if { [string tolower [HTTP::host]] eq "www.xyzabc.com"} {

 

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

 

this will redirect www.xyzabc.com to below URL

 

HTTP::redirect "https://www.xyzabc.com/xyz/web_portal/web/home/"

 

}

 

}

 

if { [string tolower [HTTP::host]] eq "xyzabc.com"} {

 

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

 

HTTP::redirect "https://www.xyzabc.com/xyz/web_portal/web/home/"

 

}

 

}

 

 

 

if { [string tolower [HTTP::host]] eq "hd.xyzabc.com"} {

 

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

 

this will redirect www.xyzabc.com to below URL

 

HTTP::redirect "https://www.xyzabc.com/xyz/web_portal/web/thuchanh_xyz/"

 

}

 

}

 

if { [string tolower [HTTP::host]] eq "www.hd.xyzabc.com"} {

 

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

 

this will redirect xyz.biz to below URL

 

HTTP::redirect "https://www.xyzabc.com/xyz/web_portal/web/thuchanh_xyz/"

 

}

 

}

 

 

 

if { [string tolower [HTTP::host]] eq "kyso.xyzabc.com"} {

 

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

 

HTTP::redirect "https://kyso.xyzabc.com/digitalsign/web/"

 

}

 

}

 

if { [string tolower [HTTP::host]] eq "www.hd.xyzabc.com"} {

 

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

 

HTTP::redirect "https://kyso.xyzabc.com/digitalsign/web/"

 

}

 

}

 

}

 

 

 

 

This is the iRule for HTTP Virtual Server and I applied the same with HTTPS VS (the main reason is all http request will need to be redirected to HTTPS).

 

 

 

This iRules' working but I found that for the 2 if, I should compose to 1 if { xxx and yyy} but did not succeed. Will this cause the iRule to be proccessed slowly? Are there any way to optimize the iRule so it can work faster?

 

 

 

Thanks and Best Regards,

 

Khoa

 

  • Hi Khoa,

     

     

    I'd replace the if statements with a switch statement. This will save you from setting the host to lower case multiple times and lower the number of checks performed if a match is found:

     

     

    
    when HTTP_REQUEST {
    
       if {[HTTP::path] eq "/"}{
          switch [string tolower [HTTP::host]] {
             "www.xyzabc.com" -
             "xyzabc.com" {
                HTTP::redirect "https://www.xyzabc.com/xyz/web_portal/web/home/"
             }
             "www.hd.xyzabc.com" -
             "hd.xyzabc.com" {
                HTTP::redirect "https://www.xyzabc.com/xyz/web_portal/web/thuchanh_xyz/"
             }
             "www.kyso.xyzabc.com" -
             "kyso.xyzabc.com" {
                HTTP::redirect "https://kyso.xyzabc.com/digitalsign/web/"
             }
             "www.kyso.xyzabc.com" -
             "kyso.xyzabc.com" {
                HTTP::redirect "https://kyso.xyzabc.com/digitalsign/web/"
             }
             default {
                 Take some default action of the HTTP path is / and there isn't a match on the host?
             }
          }
       }
    }
    

     

     

    Note that I left out the switch condition for this block of code as the host is already listed above:

     

     

    if { [string tolower [HTTP::host]] eq "www.hd.xyzabc.com"} {

     

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

     

    HTTP::redirect "https://kyso.xyzabc.com/digitalsign/web/"

     

    }

     

    }

     

     

    Aaron