Forum Discussion
Using an iRule for a 301 redirect and replace a sub string within the URI
Hello,
I am trying to do a 301 redirect to a new URL while rewriting a sub string within the original URI on a LTM using an iRule. Here is the simplified example of what I am trying to accomplish:
Original URL - xyz.com/abc/efg/*
301 Redirect URL - tuv.com/123/456/*
The wildcard sting after the sub string I want to replace must remain the same between the original URL and the 301 URL.
This is what I have written up for an iRule:
when HTTP_REQUEST {
if { [HTTP::uri] contains "/abc/efg" } {
[HTTP::host] replace "tuv.com"
[HTTP::uri] replace [string map -nocase {"/abc/efg" "/123/456"} [HTTP::uri]]
HTTP::respond 301 "https://[HTTP::host][HTTP::uri]"
}
}
Thanks in advance and any advise is greatly appreciated.
2 Replies
- Kevin_Stewart
Employee
when HTTP_REQUEST { if { [string tolower [HTTP::uri]] starts_with "/abc/efg" } { set newuri [string map -nocase {"/abc/efg" "/123/456"} [HTTP::uri]] HTTP::respond 301 "https://tuv.com${newuri}" } } - Kevin_Stewart
Employee
Right. I forgot to add the Location header:
when HTTP_REQUEST { if { [string tolower [HTTP::uri]] starts_with "/abc/efg" } { set newuri [string map -nocase {"/abc/efg" "/123/456"} [HTTP::uri]] HTTP::respond 301 Location "https://tuv.com${newuri}" } }
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