Forum Discussion
Convert Microsoft rewrite rules to iRule
Hi RiverFish,
the HTTP_REQUEST part looks good so far, but the HTTP_RESPONSE part doesn't make any sense at all.
The problem with the HTTP_RESPONSE part is, that a HTTP response does not contain any HTTP::host and HTTP::uri information. So you can't parse nor change those information nor could you even safe such an iRule on your device...
But a HTTP response may contain embedded URL information within the content of the HTTP response payload or on various other HTTP header (e.g. Location Header for redirects, Cookie directives, etc.). You may take a look to the iRule below how HTTP response content and HTTP response headers can be inspected and changed to reflect an external URL scheme.
when HTTP_REQUEST {
Disable stream engine for HTTP request
Note: You have to apply a HTTP stream and compression profile to make it work.
STREAM::disable
If client IP matches allow the request and replace the host header.
if { [IP::addr [IP::client_addr] equals 280.34.72.0/26] } then {
Store the requested HOST header value for later reference.
set orig_host [HTTP::host]
HTTP::header replace Host "server003.ab.abc.com"
Inform later iRule code to trigger if SRC IP matches.
set need_translation 1
}
Add a trailing slash to the url except if it contains a folder or filename.
set uri_base [URI::basename [HTTP::path]]
if { $uri_base equals "" } then {
The URI::basename is /folder/
} elseif { $uri_base contains "." } then {
The URI::basename is *.* file
} else {
HTTP::respond 301 "Location" "[HTTP::path]/"
}
}
when HTTP_RESPONSE {
if { $need_translation } then {
Stream the HTTP content (if contains text) and replace any internal URL with the external URL
if { [HTTP::header value "Content-Type"] contains "text/" } then {
STREAM::expression "@server003\.ab\.abc\.com/KeyManager@$orig_host@"
STREAM::enable
}
Check for Loacation headers and replace any internal URL with the external URL
if { ( [HTTP::header value "Location"] ne "" )
and ( [HTTP::header value "Location"] contains "server003.ab.abc.com/KeyManager" ) } then {
HTTP::header replace "Location" [string map "server003.ab.abc.com/KeyManager $orig_host" [HTTP::header value "Location"]]
}
}
}
Note: Depending on your application it may be required to parse and change other HTTP information to make it work.
Note: Instead of using iRules for this task you may also take a look to LTMs Rewrite Profiles. Its in most cases much easier to configure...
https://support.f5.com/csp/article/K14775
Cheers, Kai
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