Forum Discussion
iRule to redirect to new url, but retain path after first location
I have been tasked with creating an irule to redirect a url to a new server. However the existing server will still have other urls still working on it. We need to retain everything in the URL after the first path.
old url: https://www.abc.com/APP/ redirect: https://app.def.com/
and example would be OLD URL: https://www.abc.com/APP/DD_CalendarDetail.aspx?id=16888&dt=2015-09-25 REDIRECT URL: https://app.def.com/DD_CalendarDetail.aspx?id=16888&dt=2015-09-25
Would the following both redirect, and maintain the path after the /APP/ ?
when HTTP_REQUEST { if { [HTTP::host] equals "www.abc.com" and [HTTP::uri] starts_with "/APP/" } { HTTP::redirect "https://app.def.com/"
In addition, am I correct in the assumption that any urls not matching will just ignore the iRule and process normally?
14 Replies
- Stanislas_Piro2
Cumulonimbus
try this
when HTTP_REQUEST { if { [HTTP::host] equals "www.abc.com" and [HTTP::uri] starts_with "/APP/" } { HTTP::redirect "https://app.def.com/[string map {/APP/ /} [HTTP::uri]]" } } - Brad_Parker
Cirrus
For a permanent redirect you would use this to do a 301:
when HTTP_REQUEST { if { [string tolower [HTTP::host]] equals "www.abc.com" and [string tolower [HTTP::uri]] starts_with "/app/" } { HTTP::respond 301 noserver Location "https://app.def.com/[string map {/app/ /} [string tolower [HTTP::uri]]]" } }- SSell
Altostratus
Awesome, thank you. - SSell
Altostratus
We don't have anyway to test this until actual implementation. I was going to leave the question open until we implement and test which is scheduled for next Tuesday. Thanks for your help - Brad_Parker
Cirrus
You probably want to add string tolower to prevent case mis-match compares. I update above.
- Brad_Parker_139
Nacreous
For a permanent redirect you would use this to do a 301:
when HTTP_REQUEST { if { [string tolower [HTTP::host]] equals "www.abc.com" and [string tolower [HTTP::uri]] starts_with "/app/" } { HTTP::respond 301 noserver Location "https://app.def.com/[string map {/app/ /} [string tolower [HTTP::uri]]]" } }- SSell
Altostratus
Awesome, thank you. - SSell
Altostratus
We don't have anyway to test this until actual implementation. I was going to leave the question open until we implement and test which is scheduled for next Tuesday. Thanks for your help - Brad_Parker_139
Nacreous
You probably want to add string tolower to prevent case mis-match compares. I update above.
- PSritaC
Altostratus
Hello
I have a similar issue - where the redirection should retain path after the second location.
when HTTP_REQUEST {
# Do redirects.
if { ([string tolower [HTTP::host]] equals "abcshare") and ([HTTP::uri] starts_with "/site/app") } {
HTTP::respond 301 Location "https://app.abcshare.com[HTTP::uri]"
}
elseif { [string tolower [HTTP::host]] equals "abcshare" and ([HTTP::uri] starts_with "/") } {
HTTP::respond 301 Location "https://abcshare.com/"
}
}
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