Forum Discussion
URL Redirect added to existing rule
How can I add another argument so that we can redirect URL's to execute before the code below? For example, I want to have https://domain1.com/nice/main1 to redirect to https://domain1.com/v. Also to have https://domain1.com/nice/main2 redirect to https://domain1.com/y all before the rest of the code below takes place?
Code
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::uri]] {
"/nice*" -
"/green*" -
"/tree*" { pool abc }
default { pool xyz }
}}
5 Replies
- Kevin_Stewart
Employee
First keep in mind that a redirect is a physical thing, where the server sends a 30x response to the client with a Location header and a URL for the client to follow. So the client would go to:
https://domain1.com/nice/main1Get a redirect response and then go to:
https://domain1.com/vAnd that's what would show up in the browser address bar. Also consider that a redirect is a preemptive thing, so a response is issued immediately to the client and no traffic is passed to any pools. That may or may not be what you want. Also, the stateless nature of HTTP means that one client request for "/nice/main1" and another for "/v", even if from the same client, might as well be from different clients on opposite sides of the planet. That's why you need state mechanisms in HTTP, like cookies. So given that your question involves a redirect to "/v" and then an evaluation of a URI that won't be in the new request, you may need to reconsider what you're attempting to do.
- Kevin_Stewart
Employee
It's a matter of "state". So a timeline of events might look like this:
-
Client makes a new request to /nice/main1
-
iRule redirects the user to /v
-
Client makes a new request to /v and both parties are completely oblivious to the previous request. This is really the important part. You could technically set a cookie within the redirect to /v from the first request to /nice, but otherwise the new request to /v is a completely separate and new request with no knowledge of the previous request.
Any URI evaluation that takes requests to a pool must then be based on the final URI (ie. /v).
-
- Christian_30338Historic F5 Account
Try something like this....(i haven't tested the syntax)
when HTTP_REQUEST { switch -glob [HTTP::uri] { "/nice/main1" { HTTP::redirect "https://domain1.com/v" } "/nice/main2" { HTTP::redirect "https://domain1.com/y" } "/nice*" { pool abc } "/green*" { pool abc } default { pool xyz } } } - Kevin_Stewart
Employee
Agree with Christian. If the third line means anything that starts with /nice but isn't /nice/main1 or /nice/main2, then the above should cover it (a catch all for anything under /nice that isn't otherwise specified). Order of the conditions is of course crucial here.
- Kevin_Stewart
Employee
Instead of an HTTP::redirect command use the following:
HTTP::respond 301 Location "https://domain1.com/y"
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