Forum Discussion
Manuel_60430
Nimbostratus
Mar 25, 2014TCL error message with X-Forwarded-For iRule
We are using the X-Forwarded-For http header field on the BigIP LTM to server our webshop application with client IP addresses (to determinde country hawk and so on). Our web application can only wor...
hoolio
Cirrostratus
Apr 01, 2014Can you try a combined version?
when HTTP_REQUEST {
'root domain redirect'
if { [HTTP::host] eq "mydomain.com" } {
HTTP::respond 301 Location http://www.mydomain.com[HTTP::uri]
} elseif { [set redir [class lookup [string tolower [HTTP::uri]] equals REDIRECTS_301_SCO]] ne "" } {
HTTP::respond 301 Location $redir
} elseif { "[HTTP::host][HTTP::uri]" eq "asia.mydomain.com/taiwan" } {
HTTP::redirect http://www.mydomain.com/something
} elseif { "[HTTP::host][HTTP::uri]" eq "asia.mydomain.com/hongkong" } {
HTTP::redirect http://www.mydomain.com/something
} elseif { "[HTTP::host][HTTP::uri]" eq "asia.mydomain.com/china" } {
HTTP::redirect http://www.mydomain.com/something
} elseif { [HTTP::host] contains "asia.mydomain.com" } {
HTTP::redirect http://www.mydomain.com
} else {
Always need to remove any existing XFF headers.
if {[HTTP::header exists X-Forwarded-For]}{
HTTP::header remove X-Forwarded-For
}
If the ‘True-Client-IP’ header exists use it, otherwise just use client address.
if {[HTTP::header exists True-Client-IP]}{
HTTP::header replace X-Forwarded-For [HTTP::header value True-Client-IP]
} else {
HTTP::header replace X-Forwarded-For [IP::client_addr]
}
}
}
There were a few issues:
- Tcl error from trying to issue a redirect and insert an HTTP header for the same request
- [HTTP::request] returns all the HTTP headers of a request. I think you want the host header value and the URI so I used [HTTP::host][HTTP::uri] and removed the protocol from the strings you were checking
- == should be used for numeric comparisons. Use eq for string comparisons.
- You can save the output from class lookup instead of running two commands
Aaron
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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