Forum Discussion
ukstin
Nimbostratus
Jun 09, 2008conditional redirect (404)
Hi people,
I´m trying to wrote a i-rule to redirect a 404 error based on the url. The i-rule I made is this one:
when HTTP_REQUEST {
set host [HTTP::host]
set url [HTTP::uri]
}
when HTTP_RESPONSE {
set status_code [HTTP::status]
if { $status_code equals 404 } {
if { $host contains "abcd" } {
HTTP::redirect "http://abcd.otherdomain.com/$url"
}
elseif { $host contains "efg" } {
HTTP::redirect "http://efg.otherdomain.com/$url"
}
else {
HTTP::redirect "http://anotherone.com/xyz.html"
}
}
}
it seens ok to me and it works almost every time, but sometimes I received the following error:
Jun 9 19:25:07 tmm tmm[1133]: 01220001:3: TCL error: Rule irule_redirect_404 - can't read "host": no such variable while executing "if { $host contains ..."
any ideas why the host variable doesn´t work sometimes?
- The_Bhattman
Nimbostratus
Looks like the variable can't be used in another part of the irule.when HTTP_REQUEST { set ::host [HTTP::host] set ::url [HTTP::uri] } when HTTP_RESPONSE { if { [HTTP::status] == 404 } { if { $::host contains "abcd" } { HTTP::redirect "http://abcd.otherdomain.com/$::url" } elseif { $::host contains "efg" } { HTTP::redirect "http://efg.otherdomain.com/$::url" } else { HTTP::redirect "http://anotherone.com/xyz.html" } } }
- Patrick_Chang_7Historic F5 AccountSome HTTP requests do not contain a host header. Therefore, [HTTP::host] will be empty. You could change the when HTTP_REQUEST logic to read the host from the first line of the raw HTTP::request if [HTTP::host] is an empty string. I would log the value of [HTTP::host] when you set the host variable, just to make sure though. You can remove the logging when the rule is fully debugged.
- hoolio
Cirrostratus
The host header is only required for HTTP 1.1. If a client sends an HTTP 1.0 (or even 0.9) request, the Host header value wouldn't necessarily be present, but regardless the $host variable should be set to a zero length string. I'm not sure how the runtime error could be triggered for $host not being defined, as it's set on every HTTP request (even if it's to a null value).when HTTP_REQUEST { set host [HTTP::host] set url [HTTP::uri] } when HTTP_RESPONSE { if { [HTTP::status] == 404 } { if {[info exists host]}{ if {$host contains "abcd" } { HTTP::redirect "http://abcd.otherdomain.com/$url" } elseif {[info exists host] && $host contains "efg" } { HTTP::redirect "http://efg.otherdomain.com/$url" } else { HTTP::redirect "http://anotherone.com/xyz.html" } } else { $host doesn't exist! log local0. "[IP::client_addr]:[TCP::client_port]: \$host was not set!?" } } }
when HTTP_REQUEST { set host [HTTP::host] log local0. "HTTP version: [HTTP::version], HTTP host: $host, HTTP uri: [HTTP::uri]" } when HTTP_RESPONSE { if { $host contains "www" } { log local0. "matched $host" } else { log local0. "didn't match $host" } }
when HTTP_REQUEST { set http_host [HTTP::host] set http_header_host [HTTP::header Host] log local0. "HTTP version: [HTTP::version], HTTP host: $http_host, HTTP header Host: $http_header_host, HTTP uri: [HTTP::uri]" } when HTTP_RESPONSE { if { $http_host contains "www" } { log local0. "http_host matched $http_host" } else { log local0. "http_host didn't match $http_host" } if { $http_header_host contains "www" } { log local0. "http_header_host matched $http_header_host" } else { log local0. "http_header_host didn't match $http_header_host" } }
- Andy_Herrman_22
Nimbostratus
Yea, this looks really similar to the ProxyPass issue from a couple days ago. They were setting a variable in HTTP_REQUEST but occasionally they'd get a runtime error accessing it in HTTP_RESPONSE.
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