Forum Discussion

Rajesh_A_142089's avatar
Rajesh_A_142089
Icon for Nimbostratus rankNimbostratus
Jan 24, 2014

Conditional Redirection

My use case for iRule:

 

iRule on Source_Host VIP If Source_Host response is 404 then redirect to Target_Host.

 

iRule on Target_Host VIP If Current Request is a Redirect and current response is 404 then redirect to an Outage page hosted somewhere else.

 

How to achive this?

 

My initial script and the result:

 

iRule on Source_Host VIP code

 

when HTTP_REQUEST { if {[string tolower [HTTP::host]] eq "sourcehost.mydomain.com"} { set iscollabSite "Yes" set currentUri [HTTP::uri] } }

 

when HTTP_RESPONSE { if {[HTTP::status] == 404 && $iscollabSite eq "Yes"} { HTTP::redirect "https://targethost.mydomain.com$currentUri" } }

 

Result: No redirection!

 

Please advise.

 

Thanks

 

Rajesh

 

  • e.g.

    root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm virtual bar
    ltm virtual bar {
        destination 172.28.24.10:80
        ip-protocol tcp
        mask 255.255.255.255
        pool foo
        profiles {
            http { }
            tcp { }
        }
        rules {
            qux
        }
        source 0.0.0.0/0
        source-address-translation {
            type automap
        }
        vs-index 8
    }
    root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm pool foo
    ltm pool foo {
        members {
            200.200.200.101:80 {
                address 200.200.200.101
            }
        }
    }
    root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm rule qux
    ltm rule qux {
        when HTTP_REQUEST {
      if { [HTTP::host] eq "sourcehost.mydomain.com" } {
        set is_sourcehost 1
        set uri [HTTP::uri]
      } else {
        set is_sourcehost 0
      }
    }
    when HTTP_RESPONSE {
      if { $is_sourcehost and [HTTP::status] == 404 } {
        HTTP::respond 302 noserver Location "http://targethost.mydomain.com$uri"
      }
    }
    }
    
    [root@centos1 ~] curl -i http://172.28.24.10/something -H "Host: sourcehost.mydomain.com"
    HTTP/1.0 302 Found
    Location: http://targethost.mydomain.com/something
    Connection: Keep-Alive
    Content-Length: 0
    
    
  • iRule on Source_Host VIP If Source_Host response is 404 then redirect to Target_Host.

     

    can you try HTTP::respond instead of HTTP::redirect?

     

  • e.g.

    root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm virtual bar
    ltm virtual bar {
        destination 172.28.24.10:80
        ip-protocol tcp
        mask 255.255.255.255
        pool foo
        profiles {
            http { }
            tcp { }
        }
        rules {
            qux
        }
        source 0.0.0.0/0
        source-address-translation {
            type automap
        }
        vs-index 8
    }
    root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm pool foo
    ltm pool foo {
        members {
            200.200.200.101:80 {
                address 200.200.200.101
            }
        }
    }
    root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm rule qux
    ltm rule qux {
        when HTTP_REQUEST {
      if { [HTTP::host] eq "sourcehost.mydomain.com" } {
        set is_sourcehost 1
        set uri [HTTP::uri]
      } else {
        set is_sourcehost 0
      }
    }
    when HTTP_RESPONSE {
      if { $is_sourcehost and [HTTP::status] == 404 } {
        HTTP::respond 302 noserver Location "http://targethost.mydomain.com$uri"
      }
    }
    }
    
    [root@centos1 ~] curl -i http://172.28.24.10/something -H "Host: sourcehost.mydomain.com"
    HTTP/1.0 302 Found
    Location: http://targethost.mydomain.com/something
    Connection: Keep-Alive
    Content-Length: 0
    
    
    • Rajesh_A_142089's avatar
      Rajesh_A_142089
      Icon for Nimbostratus rankNimbostratus
      Thank you Nitass, your example helped me to implement what I was looking for. Here is my code example for others. -----iRule on Source Farm------- when HTTP_REQUEST { if {[string tolower [HTTP::host]] eq "source.mydomain.com"}{ set is_sourcehost 1 set uri [HTTP::uri] } else { set is_sourcehost 0 } } ----If source farm is not responding then redirect to target farm---------- when HTTP_RESPONSE { if { $is_sourcehost and [HTTP::status] == 404 } { HTTP::respond 302 noserver Location "https://target.mydomain.com$uri" } } -----End of rule-------
  • e.g.

    root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm virtual bar
    ltm virtual bar {
        destination 172.28.24.10:80
        ip-protocol tcp
        mask 255.255.255.255
        pool foo
        profiles {
            http { }
            tcp { }
        }
        rules {
            qux
        }
        source 0.0.0.0/0
        source-address-translation {
            type automap
        }
        vs-index 8
    }
    root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm pool foo
    ltm pool foo {
        members {
            200.200.200.101:80 {
                address 200.200.200.101
            }
        }
    }
    root@(ve11a)(cfg-sync In Sync)(Active)(/Common)(tmos) list ltm rule qux
    ltm rule qux {
        when HTTP_REQUEST {
      if { [HTTP::host] eq "sourcehost.mydomain.com" } {
        set is_sourcehost 1
        set uri [HTTP::uri]
      } else {
        set is_sourcehost 0
      }
    }
    when HTTP_RESPONSE {
      if { $is_sourcehost and [HTTP::status] == 404 } {
        HTTP::respond 302 noserver Location "http://targethost.mydomain.com$uri"
      }
    }
    }
    
    [root@centos1 ~] curl -i http://172.28.24.10/something -H "Host: sourcehost.mydomain.com"
    HTTP/1.0 302 Found
    Location: http://targethost.mydomain.com/something
    Connection: Keep-Alive
    Content-Length: 0
    
    
    • Rajesh_A_142089's avatar
      Rajesh_A_142089
      Icon for Nimbostratus rankNimbostratus
      Thank you Nitass, your example helped me to implement what I was looking for. Here is my code example for others. -----iRule on Source Farm------- when HTTP_REQUEST { if {[string tolower [HTTP::host]] eq "source.mydomain.com"}{ set is_sourcehost 1 set uri [HTTP::uri] } else { set is_sourcehost 0 } } ----If source farm is not responding then redirect to target farm---------- when HTTP_RESPONSE { if { $is_sourcehost and [HTTP::status] == 404 } { HTTP::respond 302 noserver Location "https://target.mydomain.com$uri" } } -----End of rule-------