Forum Discussion
Redirect when both Host and Path are equal
Hello, I have a virtual server who has many different host names and path, one of those i need to have a redirect like this: If hostname is and path is / redirect to
How shod I write the iRule? can any help me, I cannot do a redirect from / to 123 cause it will affect the other sites.
2 Replies
Hi Göran,
you may try either a nested
or[switch]
condition to parse the HOST and then the Path (general purpose) or use a combined [if] condition (tailordered purpose).[if]Example 1: Using general purpose nested
commands.[switch]when HTTP_REQUEST { switch -glob -- [string tolower [HTTP::host]] { "www.domain.de" { Requests for www.domain.com switch -glob -- [string tolower [HTTP::path]] { "/" { Requests for URI = / HTTP::redirect "/abc/" } default { Requests for URI = * } } } "www.domain.com" { Requests for www.domain.com } "www.domain.net" { Requests for www.domain.net } default { Unknown HOST-name } } }Example 2: Using general purpose nested
commands.[if]when HTTP_REQUEST { set low_host [string tolower [HTTP::host]] if { $low_host eq "www.domain.de" } then { Requests for www.domain.de set low_path [string tolower [HTTP::path]] if { $low_path equals "/" } then { Requests for URI = / HTTP::redirect "/abc/" } else { Requests for URI = * } } elseif { $low_host equals "www.domain.com" } then { Requests for www.domain.com } } elseif { $low_host equals "www.domain.net" } then { Requests for www.domain.net } else { Unknown HOST-name } }Example 3: Using a tailordered
command (to support just your requirements)[if]when HTTP_REQUEST { if { ( [string tolower [HTTP::host]] equals "www.domain.de" ) and ( [HTTP::path] equals "/" } then { Requests for www.domain.de/ HTTP::redirect "/abc/" } else { Request for another HOST-name or another Path } }Note: The general purpose examples can be easily extended to support various other things. The benefit of such a syntax is that you get scalable and unified method to control your traffic as needed.
Cheers, Kai
- PeteWhite
Employee
In general, this type of redirecting based on rules should be done using LTM policies for which you can add rules that match on multiple objects and are much more scalable than dealing with writing and rewriting iRules. They are also more performant as they are built-in and use a selection tree rather than runtime decisions. These were available in v11.4+
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
