Forum Discussion

Yolanda_Almonte's avatar
Yolanda_Almonte
Icon for Nimbostratus rankNimbostratus
Oct 09, 2013

Need iRule for 301 redirection

I would like to ask expert assistance creating an iRule on the LTM to perform a 301 redirection. The following is required:

 

  1. If requested URL contains hostname example.edu (not case sensitive) a. 301 redirect user to http://www.example.edu, and include all additional pathing. Examples: i. Example 1: I. Original request: http://example.edu/example a. Must 301 redirect to: http://www.example.edu/example ii. Example 2: II. Original request: http://example.edu/example/?user=1 a. Must 301 redirect to http://www.example.edu/example/?user=1

     

  2. If requested URL contains hostname new.example.edu (not case sensitive) a. 301 redirect user to http://www.example.edu, and include all additional pathing. Examples: i. Example 1: I. Original request: http://new.example.edu/example a. Must 301 redirect to: http://www.example.edu/example ii. Example 2: II. Original request: http://new.example.edu/example/?user=1 a. Must 301 redirect to http://www.example.edu/example/?user=1

     

I created the following iRule, however, it seems to only have taken care of the first redirection request and not any of the others: when HTTP_REQUEST { if {[HTTP::host] equals "example.edu"} { HTTP::respond 301 "Location" "http://www.example.edu" } if {[HTTP::host] equals "http://example.edu[HTTP::uri]"} { HTTP::respond 301 "Location" "http://www.example.edu[HTTP::uri]" } if {[HTTP::host] equals "new.example.edu"} { HTTP::respond 301 "Location" "http://www.example.edu" } if {[HTTP::host] equals "http://new.example.edu[HTTP::uri]"} { HTTP::respond 301 "Location" "http://www.example.edu[HTTP::uri]" } }

 

2 Replies

  • Can you try this:

    when HTTP_REQUEST {
      set r_host [HTTP::header "Host"]
      switch $r_host {
        "example.edu" -
        "new.example.edu" {
          HTTP::respond 301 "Location" "http://www.example.edu[HTTP::uri]"
        }
      }
    }
    
  • Combined iRule:

    when HTTP_REQUEST {
        switch [string tolower [HTTP::host]] {
            "example.edu" {
                HTTP::respond 301 Location "http://www.example.edu[HTTP::uri]"
            }
            "new.example.edu" {
                HTTP::respond 301 Location "http://www.example.edu[HTTP::uri]"
            }
        }
    }
    

    You wouldn't want to use a "contains" (or glob) operator here because the redirect URL contains the same host name - a potential loop.