Forum Discussion
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.
- When a url comes through from this domain, we would like to rewrite as follows:
- Persona-api.domainname.com/v1/abc/…. - Rewrite to 172.17.2.7/abc/…
- Here we remove the /v1 in the path
- Persona-api.domainname.com/v2/xyz/…. – rewrite to 172.17.19.19/xyz/…
- Here we remove the /v2 in the path
- Persona-api.domainname.com/v1/abc/…. - Rewrite to 172.17.2.7/abc/…
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
11 Replies
- Kevin_Stewart
Employee
Try this:
[string map -nocase {"/V1" "" "/V2" ""} [HTTP::uri]] - Michael_Yates
Nimbostratus
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. - farache_28983
Nimbostratus
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! - santosh_81454
Nimbostratus
Hi Farache, you are trying to parse the URI. The below link is helpful.
https://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/18/How-to-parse-a-URI.aspx
By following the examples, you should be able to create a rule that eliminates the first part.
Thanks,
Santosh. - Kevin_Stewart
Employee
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"
}
- farache_28983
Nimbostratus
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? - farache_28983
Nimbostratus
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? - Michael_Yates
Nimbostratus
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]" } } - Michael_Yates
Nimbostratus
Sorry...That won't work 🙂
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]]" } } - farache_28983
Nimbostratus
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?
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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