Forum Discussion
Convert Microsoft rewrite rules to iRule
Greetings, all. Is anyone familiar with Microsoft rewrite rules? I'm hoping someone could convert these to an iRule. These rules sit on a Microsoft ARR server acting as a reverse proxy between the cloud and the internal servers. The first two are inbound rules and the last two are outbound (response) rules. I can tell that the first inbound rule rewrites the url to the internal server and the second one adds a trailing slash unless it ends with a file name? The outbound rules appear to strip the internal server?
- RiverFishAltostratus
Here is my attempt. I'm sure it needs help. Would appreciate if someone could take a look...
when HTTP_REQUEST { If client IP matches allow the request and replace the host header. if { [IP::addr [IP::client_addr] equals 280.34.72.0/26] } { HTTP::header replace Host "server003.ab.abc.com" } 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 ""} { The URI::basename is /folder/ } elseif {$uri_base contains "."} { The URI::basename is *.* file } else { HTTP::path "[HTTP::path]/" HTTP::respond 301 "Location" "[HTTP::path]" return } } when HTTP_RESPONSE { Strip host header. if { [HTTP::host][HTTP::uri] equals "server003.ab.abc.com/KeyManager/[HTTP::uri]" { HTTP::uri "/[HTTP::uri]" Set relative paths. } elseif { [HTTP::uri contains "/[HTTP::uri]" } HTTP::uri "/[HTTP::uri]" } }
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
- Thierry_JohnNimbostratus
Hello,
I would like to convert Microsoft rewrite rules to an iRule. Can anyone help me ?
Thierry
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