Forum Discussion
Irule help for host header redirection
Hi experts, I need to write an irule which will redirect some different host headers/urls to one single-different url. After doing some digging around on devcentral i found this one should do the job -
when HTTP_REQUEST { if {[HTTP::host] equals "www.example.com" } { HTTP::redirect "https://mywebsite.com" } }
Let`s say I also need to redirect two more host headers like "abccorp.com" and "xyzmedia.ca" to the same "https://mywebsite.com" , how can i redo my above irule? Should it be with multiple "if" statements?
5 Replies
- shaggy_121467
Cumulonimbus
if it's a list of hostnames that isn't too large and won't be changing too often, i would just use some or's:
when HTTP_REQUEST { if {([string tolower [HTTP::host]] equals "www.example.com") or ([string tolower [HTTP::host]] equals "abccorp.com" or [string tolower [HTTP::host]] equals "xyzmedia.ca")} { HTTP::redirect "https://mywebsite.com" } }if you want the irule to be more flexible and extensible in regards to adding hostnames, you should use a data group. create data-group (reference in irule below as hostname-data-group) with a list of the hostnames you want redirected, and use an irule similar to:
when HTTP_REQUEST { if { class match [string tolower [HTTP::host]] equals hostname-data-group } { HTTP::redirect "https://mywebsite.com" } }- sandy16
Altostratus
Thanks Shaggy.
- shaggy
Nimbostratus
if it's a list of hostnames that isn't too large and won't be changing too often, i would just use some or's:
when HTTP_REQUEST { if {([string tolower [HTTP::host]] equals "www.example.com") or ([string tolower [HTTP::host]] equals "abccorp.com" or [string tolower [HTTP::host]] equals "xyzmedia.ca")} { HTTP::redirect "https://mywebsite.com" } }if you want the irule to be more flexible and extensible in regards to adding hostnames, you should use a data group. create data-group (reference in irule below as hostname-data-group) with a list of the hostnames you want redirected, and use an irule similar to:
when HTTP_REQUEST { if { class match [string tolower [HTTP::host]] equals hostname-data-group } { HTTP::redirect "https://mywebsite.com" } }- sandy16
Altostratus
Thanks Shaggy.
- Sheigh_65772
Cirrus
Here's another options as well, really just comes down to how much data you got and how you want to manage it. Personally I like the second options Shaggy suggested.
when HTTP_REQUEST { set host [string tolower [HTTP::host]] switch $host { "site1.com" - "site2.com" - "www.site3.com" { HTTP::redirect "https://mywebsite.com" } } }
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