Rewrite to URL and strip part of the URI

Hi guys,

Basically i need to do this.. been testing different scenarios with HTTP_request and HTTP_RESPONSE but no luck.

  1. When a url comes through from this domain, we would like to rewrite as follows:
    1. Persona-api.domainname.com/v1/abc/…. - Rewrite to 172.17.2.7/abc/…
      1. Here we remove the /v1 in the path
    2. Persona-api.domainname.com/v2/xyz/…. – rewrite to 172.17.19.19/xyz/…
      1. Here we remove the /v2 in the path

Basically the only part of the URL that I know is persona-api.domainname.com/v1 or persona-api.domainname.com/v2

the rest after /V1 or /V2 will be dynamic and will change anytime..

How can I remove /V1 or /V2 and append the rest of the requested URL?

Any ideas are welcome..

thanks

Try this:

[string map -nocase {“/V1” “” “/V2” “”} [HTTP::uri]]

I would add to what Kevin Stewart told you.

His suggestion will not change the URL as it is shown in the browser. It will change what is sent to the server.

This method falls directly into the URI Masking Territory.

If you want the browser URL Changed you can take the result of Kevin’s suggest and trigger a redirect to the new location.

but how will I append the rest of the URI . the piece after /V1 or /V2.. like /V2/xyz so it looks like domain./xyz instead of domain.com/v2/xyz

that is where i am getting lost..

thank for the help!

Hi Farache, you are trying to parse the URI. The below link is helpful.

By following the examples, you should be able to create a rule that eliminates the first part.

Thanks,

Santosh.

The string map command replaces content within the given file, in this case the HTP::uri.

So using the supplied syntax:

{"/V1" "" "/V2" ""}

persona-api.domainname.com/v2/xyz/ becomes Persona-api.domainname.com/xyz/

To amplify Michael’s comments to my original post, you can either set this in an HTTP::uri statement, which conceals the true path from the user, or simply redirect them to the new path:

if { ( [string tolower [HTTP::uri]] contains "/v1 ) or ( [string tolower [HTTP::uri]] contains "/v2" ) } {
    set uri [string map -nocase {"/V1" "" "/V2" ""}]
    HTTP::redirect "http://[HTTP::host]$uri"
}

Frist of all thnkas for the help

I’ve been trying to get this one to work but everytime this rule is giving me an error

01070151:3: Rule [/Common/persona-api_irule] error:

line 5: [string map list should have an even number of elements] [-nocase]

I’ve researched and seem that I need a bracket

something like this:

when HTTP_REQUEST {

log local0. “in HTTP_REQUEST”

if { ( [string tolower [HTTP::uri]] contains “/v1” ) or ( [string tolower [HTTP::uri]] contains “/v2” ) } {

set uri {string map --nocase {“/v1” “” “/v2” “”}}

HTTP::redirect “http://[HTTP::host]$uri”

}

}

But when i run that, I get an error in the browser with the following:

http://persona-api.grey.comstring%20map%20%20--nocase%20%7B%22/v1%22%20%22%22%7D

Now to make my case worst,

I have to actually redirect the traffic to specific pools depending where they are coming from --- if it is v1 , then pool a if it is v2 then pool be

I’ve tried changing the HTTP:redirect for a swtich command but nothing

something like this

when HTTP_REQUEST {

log local0. “in HTTP_REQUEST”

if { ( [string tolower [HTTP::uri]] contains “/v1” ) } {

set uri {string map --nocase {“/v1” “”}}

switch [string tolower “http://[HTTP::host]$uri”] {

“persona.api.grey.com” {

pool persona-api-V1_pool

}

}

}

elseif { ( [string tolower [HTTP::uri]] contains “/v2” ) } {

set uri {string map --nocase {“/v2” “”}}

switch [string tolower “http://[HTTP::host]$uri”] {

“persona.api.grey.com” {

pool persona-api-V2_pool

}

}

}

}

What am i doing wrong?

After looking at this , looks like the

set uri {string map -nocase {“/v1” “” “/v2” " "} is appending these characters after the host field.

http://persona-api.domain.comstring%20map%20%7B/v1%20%22%22%20/v2%20%22%22%7D

can’t seem to find why..

any ideas?

Hi farache,

You are setting the literal value of the uri variable.

Try something like this:

when HTTP_REQUEST {
if { ( [string tolower [HTTP::uri]] contains "/v1" ) or ( [string tolower [HTTP::uri]] contains "/v2" ) } {
HTTP::uri [string map {"/v1" "" "/v2" ""} [HTTP::uri]]
HTTP::redirect "http://[HTTP::host][HTTP::uri]"
}
}

Sorry…That won’t work :slightly_smiling_face:

You could use the above to set the Variable like this:

set uri [HTTP::uri [string map {“/v1” “” “/v2” “”} [HTTP::uri]]]

Then you could do the following:

HTTP::redirect “http://[HTTP::host]$uri”

Or you could l just do this:

when HTTP_REQUEST { if { ( [string tolower [HTTP::uri]] contains "/v1" ) or ( [string tolower [HTTP::uri]] contains "/v2" ) } { HTTP::redirect "http://[HTTP::host][string map {"/v1" "" "/v2" ""} [HTTP::uri]]" } }

After looking at this , looks like the

set uri {string map -nocase {“/v1” “” “/v2” " "} is appending these characters after the host field.

http://persona-api.domain.comstring%20map%20%7B/v1%20%22%22%20/v2%20%22%22%7D

can’t seem to find why..

any ideas?

Hi farche,

You are setting the value of the $uri variable to the value in the brakets: string map -nocase {“/v1” “” “/v2” " "}

It is then appending the value to the end of your HTTP::host value. It appears like it is appending it to the HTTP::host value but it really isn’t.

Change your set value to this and it should clear your problem:

set uri [HTTP::uri [string map {“/v1” “” “/v2” “”} [HTTP::uri]]]

So the entire iRule should look like this:

when HTTP_REQUEST {
if { ( [string tolower [HTTP::uri]] contains "/v1" ) or ( [string tolower [HTTP::uri]] contains "/v2" ) } {
set uri [HTTP::uri [string map {"/v1" "" "/v2" ""} [HTTP::uri]]]
HTTP::redirect "http://[HTTP::host]$uri"
}
}