Forum Discussion

Terry_Pike's avatar
Terry_Pike
Icon for Nimbostratus rankNimbostratus
Apr 07, 2017

iRule to redirect with an elseif statement

hi Dev's Want an iRule to detect the URL – http://xxsupplier.xx.com/supplierguide and re-direct it to https://xxsupplier.xx.com/supplier_guide.html But, if the user enters just http://xxsupplier.xx.com we want them to be redirected to the HTTPS VIP of the same name

 

I have the following iRule, however it does not seem to execute the 2nd part. It WILL detect http://xxsupplier.xx.com/supplierguide and redirect to https://xxsupplier.xx.com/supplier_guide.html However if the user simply enters http://xxsupplier.xx.com the request in the browser will say ‘cannot connect”

 

Thus, my conclusion is that the ‘elseif’ part of my iRule isn’t working.

 

Any help greatly appreciated.

 

There are VIPS defined on both 443 and 80 with the same IP address.

 

when HTTP_REQUEST { set host [HTTP::host] if { [HTTP::host] equals "xxsupplier.xx.com" and [HTTP::uri] starts_with "/supplierguide" } { HTTP::redirect "; } elseif { [HTTP::host] } { HTTP::respond 302 Location "https://$host/" } }

 

6 Replies

  • Hi Terry,

    If you are getting the connection drop that is a tell-tale sign of your iRule having a bug somewhere, so it is useful to check the /var/log/ltm for any error messages. In your case it looks like you used eleseif, but forgot the condition, I assume you wanted that line to be

    elseif { [HTTP::host] equals "xxsupplier.xx.com" } 
    

    Also, get rid of the semicolon before elseif

    Hope this helps,

    Sam

  • make sure you change the host string as well as the uri string to lower case Then you can do a proper check.

     

    when HTTP_REQUEST {

     

    chage host to all lower case

    set $host [string tolower [HTTP::host]]

     

    chage uri to all lower case

    set Vuri [string tolower [HTTP::uri]]

     

    if { $host equals "xxsupplier.xx.com" and $Vuri starts_with "/supplierguide" } { HTTP::redirect ";; } elseif { $host equals "xxsupplier.xx.com" and $Vuri equals "/" } { HTTP::redirect ";}

     

    }

     

    • Hectorm_262752's avatar
      Hectorm_262752
      Icon for Cirrus rankCirrus

      You will need an IRule for the HTTP to HTTPS traffic in the Port 80

      when HTTP_REQUEST { set Vuri [ string tolower [HTTP::uri]] set Vheader [string tolower [HTTP::host]]

      Make sure you change the header and the default redirection to match the new site
      switch $Vheader {
         xxsupplier.xx.com {HTTP::redirect "https://[HTTP::host][HTTP::uri]"}
      default { HTTP::redirect "https://xxsupplier.xx.com/" }
      }
      

      }

      Then I will apply the following Irule to the HTTPS

      when HTTP_REQUEST {

      chage host to all lower case

      set $host [string tolower [HTTP::host]]

      chage uri to all lower case

      set Vuri [string tolower [HTTP::uri]]

      if { $host equals "xxsupplier.xx.com" and $Vuri starts_with "/supplierguide" } { HTTP::redirect ";; } elseif { $host equals "xxsupplier.xx.com" and $Vuri equals "/" } { HTTP::redirect ";}

      }

  • make sure you change the host string as well as the uri string to lower case Then you can do a proper check.

     

    when HTTP_REQUEST {

     

    chage host to all lower case

    set $host [string tolower [HTTP::host]]

     

    chage uri to all lower case

    set Vuri [string tolower [HTTP::uri]]

     

    if { $host equals "xxsupplier.xx.com" and $Vuri starts_with "/supplierguide" } { HTTP::redirect ";; } elseif { $host equals "xxsupplier.xx.com" and $Vuri equals "/" } { HTTP::redirect ";}

     

    }

     

    • Hectorm's avatar
      Hectorm
      Icon for Nimbostratus rankNimbostratus

      You will need an IRule for the HTTP to HTTPS traffic in the Port 80

      when HTTP_REQUEST { set Vuri [ string tolower [HTTP::uri]] set Vheader [string tolower [HTTP::host]]

      Make sure you change the header and the default redirection to match the new site
      switch $Vheader {
         xxsupplier.xx.com {HTTP::redirect "https://[HTTP::host][HTTP::uri]"}
      default { HTTP::redirect "https://xxsupplier.xx.com/" }
      }
      

      }

      Then I will apply the following Irule to the HTTPS

      when HTTP_REQUEST {

      chage host to all lower case

      set $host [string tolower [HTTP::host]]

      chage uri to all lower case

      set Vuri [string tolower [HTTP::uri]]

      if { $host equals "xxsupplier.xx.com" and $Vuri starts_with "/supplierguide" } { HTTP::redirect ";; } elseif { $host equals "xxsupplier.xx.com" and $Vuri equals "/" } { HTTP::redirect ";}

      }

  • Hi Terry,

    you may take a look to the iRule below. The rules uses two nested [if] statements to avoid variable usage.

    when HTTP_REQUEST {
        if { [string tolower [HTTP::host]] equals "xxsupplier.xx.com" } then { 
            if { [string tolower [HTTP::uri]] starts_with "/supplierguide" } then {  
                HTTP::respond 302 Location "https://xxsupplier.xx.com/supplier_guide.html"
            } else {  
                HTTP::respond 302 Location "https://xxsupplier.xx.com[HTTP::uri]" 
            }
        } else {
             Request for unknown host name.        
        }
    }
    

    Cheers, Kai