Forum Discussion
CAMES_13251
Jun 20, 2011Nimbostratus
Remove URI if it doesn't match my options?
Hi All,
I'm quite new to irules, and can't get something working that should be quite simple.
What I am trying to do is redirect the request based on a couple of key words in the URI, but if the URI does not contain one of these, remove the URI completely and just go to the main site.
So, if the host is www.test.com...
http://www.test.com/ - would go to www.test.com
http://www.test.com/test1 - would go to www.testone.com
http://www.test.com/test2 - would got to www.testtwo.com
And any other URI would result in it just going to www.test.com
Here is the irule I am trying...
when HTTP_REQUEST {
Check the requested URI set to lowercase
switch -glob "[string tolower [HTTP::uri]]" {
"/test1" {
Requested URI /test1
HTTP::respond 301 Location "http://www.testone.com/"
}
"/test2" {
Requested URI /test2
HTTP::respond 301 Location "http://www.testtwo.com/"
}
}
if {[HTTP::uri] ne "/"} then {[HTTP::uri] "/"}
}
Any idea what I am doing wrong? I assume it is related to the if statement after the switch, as the switch seems to work.
Thanks in advance!
- hooleylistCirrostratusIf you want to set the URI you'd use HTTP::uri "/new_uri". The square braces force the interpreter to run HTTP::uri as a command and substitute the value. So you'd get a runtime error with that.
when HTTP_REQUEST { Check the requested URI set to lowercase switch -glob "[string tolower [HTTP::uri]]" { "/test1" { Requested URI /test1 HTTP::respond 301 Location "http://www.testone.com/" } "/test2" { Requested URI /test2 HTTP::respond 301 Location "http://www.testtwo.com/" } default { HTTP::uri "/" } } }
- CAMES_13251NimbostratusThanks for that, Aaron - Seems to work fine with your changes!
- CAMES_13251NimbostratusSorry to drag this old question up again, but say I wanted to preserve the URI and add it to the end of the redirection, how would I do that?
- Ryan_Paras_7933NimbostratusI just tested this and it worked:
- CAMES_13251NimbostratusThanks for the reply, Ryan.
- Michael_YatesNimbostratusAs soon as you do a 301 Redirect the traffic is gone and can no longer be affected by this iRule.
- CAMES_13251NimbostratusThat makes sense. Thanks for the help!
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