Forum Discussion
iRule switch confusion (Version 9.4.4)
So I've created an iRule using the switch statement, but for some reason my logic is failing for every conceivable match. Every condition falls through to the default case. What am I doing wrong? Thanks in advance.
New iRule to protect website storefronts from mischief
when HTTP_REQUEST {
if {[HTTP::uri] starts_with "/storefront/"} {
Do nothing
} else {
switch {[HTTP::uri]} {
"" -
"/" -
"/storefront" { HTTP::respond 301 Location "https://[HTTP::host]/storefront/home.ep" }
"/us.website-sitemap.xml" -
"/uk.website-sitemap.xml" -
"/ca.website-sitemap.xml" -
"/ie.website-sitemap.xml" -
"/au.website-sitemap.xml" -
"/row.website-sitemap.xml" -
"/robots.txt" {}
default { HTTP::respond 301 Location "https://[HTTP::host]/storefront/error.ep?errorCode=404" }
}
}
}
- Gbps_31870
Nimbostratus
Hi Aaron,
Try this ,,,
when HTTP_REQUEST {
if {[HTTP::uri] starts_with "/storefront/"} {
return
} else {
switch {[HTTP::uri]} {
"" -
"/" -
"/storefront" { HTTP::respond 301 Location "https://[HTTP::host]/storefront/home.ep" }
"/us.website-sitemap.xml" -
"/uk.website-sitemap.xml" -
"/ca.website-sitemap.xml" -
"/ie.website-sitemap.xml" -
"/au.website-sitemap.xml" -
"/row.website-sitemap.xml" -
"/robots.txt" { return }
default { HTTP::respond 301 Location "https://[HTTP::host]/storefront/error.ep?errorCode=404" }
}
}
}
- That change had no effect. The problem doesn't appear to be an issue of the conditional block being executed correctly, it's that the condition isn't matching at all.
- Kevin_Stewart
Employee
The curly braces around your switch condition causes it fail.when HTTP_REQUEST { if { [string tolower [HTTP::uri]] starts_with "/storefront/" } { log local0. "/storefront/" return Do nothing } else { switch [string tolower [HTTP::uri]] { "/" - "/storefront" { log local0. "/storefront" HTTP::respond 301 Location "https://[HTTP::host]/storefront/home.ep" } "/us.website-sitemap.xml" - "/uk.website-sitemap.xml" - "/ca.website-sitemap.xml" - "/ie.website-sitemap.xml" - "/au.website-sitemap.xml" - "/row.website-sitemap.xml" - "/robots.txt" { log local0. "xml" return } default { log local0. "default" HTTP::respond 301 Location "https://[HTTP::host]/storefront/error.ep?errorCode=404" } } } }
- Michael_Yates
Nimbostratus
Hi Aaron,when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { "/" { HTTP::respond 301 Location "https://[HTTP::host]/storefront/home.ep" } "/storefront*" - "*/us.website-sitemap.xml" - "*/uk.website-sitemap.xml" - "*/ca.website-sitemap.xml" - "*/ie.website-sitemap.xml" - "*/au.website-sitemap.xml" - "*/row.website-sitemap.xml" - "*/robots.txt" { return } default { HTTP::respond 301 Location "https://[HTTP::host]/storefront/error.ep?errorCode=404" } } }
- Thanks, +Kevin Stewart, it was the braces that was killing my match condition.
- Kevin_Stewart
Employee
It's actually a function of the browser and the HTTP protocol. Your browser will implicitly add the "/" to your request if you don't supply a path. In fact the protocol requires a path in the HTTP method statement (ex. GET / HTTP/1.1)
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