Forum Discussion
Tyranon_113005
Nimbostratus
Jan 15, 2018HTTP redirect by HOST and URL
Hello,
I'm trying redirect HTTP request depends on HTTP host and url.
Example,
https://www.aaa.com/123 -> https://www.bbb.com/specified1
https://www.aaa.com/456 -> https://www.bbb.com/specified2
https://www.aaa.com/* -> https://www.aaa.com/*
https://www.bbb.com/* -> https://www.bbb.com/*
Does the below work as I expected?
when HTTP_REQUEST {
if { [HTTP::host]] equals "www.aaa.com"}{
switch [HTTP::uri] {
"/123" { HTTP::redirect "https://www.bbb.com/specified1" }
"/456" { HTTP::redirect "https://www.bbb.com/specified2" }
}
default {
HTTP::redirect "https://[HTTP::host][HTTP::uri]"
}
}
elseif { [HTTP::host] equals "www.bbb.com" }{
HTTP::redirect "https://[HTTP::host][HTTP::uri]"
}
}
- Stanislas_Piro2
Cumulonimbus
Your code will loop!
If you want the request to be forwarded to the server, don't use redirect command!
when HTTP_REQUEST { if { [HTTP::host]] equals "www.aaa.com"}{ switch [HTTP::uri] { "/123" { HTTP::redirect "https://www.bbb.com/specified1" } "/456" { HTTP::redirect "https://www.bbb.com/specified2" } } default { forward the request... no action } } }
- oguzy
Cirrostratus
Hi Yuri,
You can try the below code.
Code
when HTTP_REQUEST { if { ([HTTP::host] equals "www.aaa.com")}{ switch [HTTP::uri] { "/123" { HTTP::redirect "https://www.bbb.com/specified1" } "/456" { HTTP::redirect "https://www.bbb.com/specified2" } default { HTTP::redirect "https://www.bbb.com[HTTP::uri]" } } } }
- BigD_300005
Cirrostratus
Just going to put this here. I always say to use a policy if you can and there is no reason you couldn't use them here.
https://devcentral.f5.com/articles/what-is-http-part-ix-policies-and-irules-29141
Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects