Forum Discussion

Sabir_Alvi's avatar
Sabir_Alvi
Icon for Altocumulus rankAltocumulus
May 27, 2021
Solved

HTTP::redirect doesn't work for multiple conditions, URL redirect doesn't work using iRule

So I have 2 types of redirects to handle, based on 2 types of URI. When a URI in the HTTP_REQUEST matches the value in Datagroup 1, I want to redirect to a certain URL by retaining a certain part of the URI.

When the URI does not match the value in Datagroup 1, it should move to the next Datagroup and look for matches in Datagroup 2. If matches, then redirect to another type of URL.

 

I have below iRule, but only the first part of the iRule is getting executed when it matches the URI. It doesn't go to the second part to look for matches. Please advise fixing this. Basically, I want to invoke the command multiple times while processing a single HTTP request.

 

when HTTP_REQUEST {
    set url_hosts [class match -value -- [HTTP::uri] starts_with DATAGROUP_1]
    if { $url_hosts ne "" } {
      set redirected "true"
      set LinkID [URI::query [HTTP::uri] id]
      HTTP::redirect "https://$url_hosts/links/$LinkID"
    }
    if { $redirected ne "true" } {
    set url_hosts [class match -value -- [HTTP::uri] starts_with DATAGROUP_2]
      if { $url_hosts ne "" } {
        set redirected "true"
        HTTP::redirect "https://$url_hosts"
      }
  }
  if { $redirected ne "true" } {
    HTTP::respond 404 content "<Some HTML Code for error page>"
  }
}


  • Hi Sabir Alvi,

     

    "redirected" variable must be defined before running code for line 8 and line 15.

    If the first if does not match, iRule will give an error, because the "redirected" variable is not defined.

    when HTTP_REQUEST {
    	set redirected "false"
    	set url_hosts [class match -value -- [HTTP::uri] starts_with DATAGROUP_1]
    	if { $url_hosts ne "" } {
    		set redirected "true"
    		set LinkID [URI::query [HTTP::uri] id]
    		HTTP::redirect "https://$url_hosts/links/$LinkID"
    	}
    	if { $redirected ne "true" } {
    		set url_hosts [class match -value -- [HTTP::uri] starts_with DATAGROUP_2]
    		if { $url_hosts ne "" } {
    			set redirected "true"
    			HTTP::redirect "https://$url_hosts"
    		}
    	}
    	if { $redirected ne "true" } {
    		HTTP::respond 404 content "<Some HTML Code for error page>"
    	}
    }

     

2 Replies

  • Hi Sabir Alvi,

     

    "redirected" variable must be defined before running code for line 8 and line 15.

    If the first if does not match, iRule will give an error, because the "redirected" variable is not defined.

    when HTTP_REQUEST {
    	set redirected "false"
    	set url_hosts [class match -value -- [HTTP::uri] starts_with DATAGROUP_1]
    	if { $url_hosts ne "" } {
    		set redirected "true"
    		set LinkID [URI::query [HTTP::uri] id]
    		HTTP::redirect "https://$url_hosts/links/$LinkID"
    	}
    	if { $redirected ne "true" } {
    		set url_hosts [class match -value -- [HTTP::uri] starts_with DATAGROUP_2]
    		if { $url_hosts ne "" } {
    			set redirected "true"
    			HTTP::redirect "https://$url_hosts"
    		}
    	}
    	if { $redirected ne "true" } {
    		HTTP::respond 404 content "<Some HTML Code for error page>"
    	}
    }

     

    • Sabir_Alvi's avatar
      Sabir_Alvi
      Icon for Altocumulus rankAltocumulus

      Thanks a lot, this just worked the way I wanted to!