iRule that match rewrite policy

Hi guys,

I need to implement an iRule that works like a Rewrite profile but should change URI path (remove a part of the string) to the server side. I cannot use the normal rewrite because on the same VIP I’ve two domains and the server URI it’s the same for both, this is an example:

prod.example.com/prod/service1 → should rewrite to /service1 when goes to server pool

test.example.com/test/service1 → should rewrite to /service1 when goes to server pool

obviously the policy gives me error backup server URI is already used.

I’ve tried this iRule but I got error 404 from server side:

when HTTP_REQUEST {

set host [HTTP::host]

set uri [string tolower [HTTP::uri]]

if { $host equals “test.example.com” } {

HTTP::uri [string map {“/test” “” } $uri ]

}

}

any help is really appreciated

thanks

Your iRule looks like it is only covering one of the two possible host names - test.example.com. There is no condition for removing the “/prod” string if the host name is prod.example.com which may be the cause of your 404 response. You can add a couple of log commands to the iRule to see what the before and after URIs look like. For example:

when HTTP_REQUEST {
    log local0. "URI on client side is: [HTTP::uri]"
    if { [HTTP::host] equals "prod.example.com" || [HTTP::host] equals "test.example.com" } {
        HTTP::uri [string map { "/test" "" } [HTTP::uri] ]
        log local0. "URI on server side changed to: [HTTP::uri]"
    }
}

Also, if there is any chance the URI might contain only “/test” or “/prod” (e.g. “test.example.com/test” or “prod.example.com/prod”) you should check for that possibility before executing the string map command as shown. Otherwise, it will leave you with a null URI which generates an error message to /var/log/ltm and stops the iRule from completing.