Forum Discussion
How to get domain name in iRule?
I have a redirect rule that I need to apply to about 30 different FQDNs:
foo.domain1.com/XYZ -> bar.domain1.com/XYZ
foo.domain2.com/XYZ -> bar.domain2.com/XYZ
foo.domain3.com/XYZ -> bar.domain3.com/XYZ
I could do this explicitly in the irule or enter all domains using a data group list, but was hoping there would be a way to get the domain automatically. Something like:
when HTTP_REQUEST {
if { [HTTP::host] starts_with "foo" } {
HTTP::redirect https://bar.[DOMAIN]/[HTTP::uri]
}
}
Anyone had to do this, and what's the best way?
5 Replies
- cjunior
Nacreous
Hi,
If you do not need to treat the domain name, you could do this with the "string map" statement.when HTTP_REQUEST { if { [HTTP::host] starts_with "foo." } { HTTP::redirect "https://[string map -nocase {foo. bar.} [HTTP::host]][HTTP::uri]" } }otherwise, you could treat it with "string range" and "string first" statement.
when HTTP_REQUEST { if { [HTTP::host] starts_with "foo." } { set dotdomain [string range [HTTP::host] [string first . [HTTP::host]] end] set host bar$dotdomain set url https://$host[HTTP::uri] log local0. "Domain $dotdomain | Host: $host | Url: $url" HTTP::redirect $url } }It's just a tip, I hope it helps you. Regards
- Vernon_97235Historic F5 Account
Certainly. Firstly, I assume you want to make sure that you match "foo" only when it is the complete name leaf, and outside of that, you will substitute any domain name which contains that leaf:
when HTTP_REQUEST { if { [HTTP::host] starts_with "foo." } { HTTP::redirect "https://bar.[string range [HTTP::host] 4 end][HTTP::uri] } }The
in the4
command is the length of the leaf name and its following dot (string range
, in this example). Notice, as well, that the leading slash (foo.
) is not needed before/
(although, strictly speaking, a user-agent could pass the protocol and/or host part in the Request-URI, but that's an entirely different problem).HTTP::uri- John_Heyer_1508
Cirrostratus
That's another nice solution. Really all depends on what's more readible. And yes, realized the URI will always start with a / so no need to have an extra one in there.
- VernonWells
Employee
Certainly. Firstly, I assume you want to make sure that you match "foo" only when it is the complete name leaf, and outside of that, you will substitute any domain name which contains that leaf:
when HTTP_REQUEST { if { [HTTP::host] starts_with "foo." } { HTTP::redirect "https://bar.[string range [HTTP::host] 4 end][HTTP::uri] } }The
in the4
command is the length of the leaf name and its following dot (string range
, in this example). Notice, as well, that the leading slash (foo.
) is not needed before/
(although, strictly speaking, a user-agent could pass the protocol and/or host part in the Request-URI, but that's an entirely different problem).HTTP::uri- John_Heyer_1508
Cirrostratus
That's another nice solution. Really all depends on what's more readible. And yes, realized the URI will always start with a / so no need to have an extra one in there.
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
