Forum Discussion
Joe_Pipitone
Nimbostratus
Mar 16, 2010Rewriting - syntax
I want to be able to catch http://www.domainname.com/anyurl/anypage.aspx or http://domainname.com/anyurl/anypage.aspx- both with and without the www and rewrite as needed. I am currently using an iRule now which works fine, however I've added another if statement.
I am simply rewriting to another domain while still keeping the uri intact.
Can anyone tell me if this syntax is correct, or should I be using else if, rather than the 2nd if?
when HTTP_REQUEST {
if { ([HTTP::host] eq "www.domainname.com") } {
switch -glob [HTTP::host] {
"/" { HTTP::redirect "http://anotherdomain.com" }
default { HTTP::redirect "http://anotherdomain.com[HTTP::uri]" }
}
}
if { ([HTTP::host] eq "domainname.com") } {
switch -glob [HTTP::host] {
"/" { HTTP::redirect "http://anotherdomain.com" }
default { HTTP::redirect "http://anotherdomain.com[HTTP::uri]" }
}
}
}
- Hamish
Cirrocumulus
For performance reasons I'd suggest an else in there... However you may also want to investigate using a switch statement, or using a class (DataGroup) and the findclass/class commands to lookup the recieved host and return the redirection host (The findclass should scale better, and mean you don't have to re-write your iRule whenever you add a new host).when HTTP_REQUEST { set redirecthost [class search -value "theclass" equals [HTTP::host]] if { $redirecthost ne "" } { HTTP::redirect $redirecthost } }
- Joe_Pipitone
Nimbostratus
We are running v 9.4.7. - Joe_Pipitone
Nimbostratus
This triggers an error - forgive me, I'm not too familiar with syntax:01070151:3: Rule [cplanning-rewrite] error: line 8: [undefined procedure: elseif] [elseif { ([HTTP::host] eq "ourdomain.com") } { switch -glob [HTTP::host] { "/" { HTTP::redirect "http://anotherdomain.com" } default { HTTP::redirect "http://anotherdomain.com[HTTP::uri]" } } } ]
when HTTP_REQUEST { if { ([HTTP::host] eq "www.ourdomain.com") } { switch -glob [HTTP::host] { "/" { HTTP::redirect "http://anotherdomain.com" } default { HTTP::redirect "http://anotherdomain.com[HTTP::uri]" } } } elseif { ([HTTP::host] eq "ourdomain.com") } { switch -glob [HTTP::host] { "/" { HTTP::redirect "http://anotherdomain.com" } default { HTTP::redirect "http://anotherdomain.com[HTTP::uri]" } } } }
- The_Bhattman
Nimbostratus
Hi Joe,when HTTP_REQUEST { switch -glob [HTTP::host] { "ourdomain.com" - "www.ourdomain.com" { HTTP::redirect "http://anotherdomain.com[HTTP::uri]" } } }
- Joe_Pipitone
Nimbostratus
So the "-" in between the hosts means either or? - hoolio
Cirrostratus
Hi Joe,when HTTP_REQUEST { switch [string tolower [HTTP::host]] { "ourdomain.com" - "www.ourdomain.com" { "http://anotherdomain.com[HTTP::uri]" } } }
- Joe_Pipitone
Nimbostratus
A big thank you to everyone who helped out. I appreciate your time!
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