Forum Discussion
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:
-
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
-
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
- Mohamed_Lrhazi
Altocumulus
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]" } } } - Kevin_Stewart
Employee
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.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com