Forum Discussion
Help with a redirect while changing a small part of the URI
I am trying to do a redirect which will look like below:
hostname.domain.com/abc/varying_remainder_uri redirect to NEWhostname.domain.com/def/varying_remainder_uri
I did the below which I know is not correct:
when HTTP_REQUEST {
if { [HTTP::uri] contains "abc"} {
HTTP::redirect "]"
}
}
It does the redirect, but keeps the "abc" in the uri so the resulting redirect looks like this:
https://NEWhostname.domain.com/def/...der_of_URI
4 Replies
- Kevin_Stewart
Employee
Assuming the /abc/ and /def/ portions of the URI are static, you could use some string mapping:when HTTP_REQUEST { if { [string tolower [HTTP::uri]] starts_with "/abc"} { set newuri [string map {"/abc/" "/def/"} [string tolower [HTTP::uri]]] HTTP::redirect "https://NEWhostname.domain.com$newuri" } } - PeterC_70986
Nimbostratus
Thanks Kevin, Yes the abc and def parts of the uri are static. I tried the rule you listed, but it does not seem to be working.
- Kevin_Stewart
Employee
Let's try some logging then to verify if the iRule is producing what you want:when HTTP_REQUEST { log local0. "original URL = [HTTP::host][HTTP::uri]" if { [HTTP::uri] starts_with "/abc/"} { set newuri [string map {"/abc/" "/def/"} [string tolower [HTTP::uri]]] log local0. "new URL = https://NEWhostname.domain.com$newuri" HTTP::redirect "https://NEWhostname.domain.com$newuri" } }
Please report back what the logs return. - PeterC_70986
Nimbostratus
Scratch that Kevin, I went back and tried it again before putting the logging on and it worked fine. Maybe something was "stuck" in my browser session that cleared. Thanks so much for the help!!
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