Forum Discussion

Joe_Pipitone's avatar
Joe_Pipitone
Icon for Nimbostratus rankNimbostratus
Jan 26, 2011

Stripping part of URI and integrating with iRule

I have to perform the following:

http://www.oursite.org/directory1/directory2/somepage.aspx needs to rewrite to http://oursite.org/somepage.aspx - essentially just stripping out directory1 and directory2.

I also need to be able to strip the www as well.

I've tried the following iRule that I found as hoolio's example, however it seems to end up getting stuck in a redirect loop.

I also do not want to string map, I need to actually rewrite the URI.

Can anyone tell me what I might be doing wrong? The bigip log shows:

Original URI: /directory1/directory2/somepage.aspx

Current URI: /somepage.aspx

 

Which seems to be correct, however redirect loop still occurs.

when HTTP_REQUEST {

log local0. "[IP::client_addr]:[TCP::client_port]: Original URI: [HTTP::uri]"

switch -glob [HTTP::path] {

"/directory1/directory2*" {

HTTP::uri [string map {"/directory1/directory2/" "/"} [HTTP::uri]]

HTTP::redirect "http://oursite.org[HTTP::uri]"

}

default {

HTTP::redirect "http://oursite.org"

}

}

}

when HTTP_REQUEST priority 501 {

log local0. "[IP::client_addr]:[TCP::client_port]: Current URI: [HTTP::uri]"

}

I then need to integrate this functionality into the bottom of this iRule, I've stripped most of it as it is very long. The following is at the very end of a large switch statement, and needs to be preserved

default {

if {[HTTP::uri] starts_with "/admin" } {

HTTP::redirect "http://legacy.oursite.org[HTTP::uri]"

} elseif {[string tolower [HTTP::host]] eq "www.oursite.org"}{

HTTP::redirect "http://oursite.org[HTTP::uri]"

}

}

Much thanks for any help!

  • Oh, and before I forget. You should use "string tolower" to convert your URI to lower case before checking it. Otherwise, someone accidentally capitalizing any characters in their request will keep the rule from executing properly.

    when HTTP_REQUEST {
      if { [string tolower[HTTP::uri]] starts_with "/directory1/directory2" } {
            HTTP::redirect "http://oursite.org/[string range [HTTP::uri] 11 end ]" 
            log local0. "/directory1/directory2 found, redirected"
       } 
    }
    
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Now that's some prompt help. Thanks Chris for all your help around here.

     

     

    Colin