Forum Discussion
removing part of uri in redirect
We are moving servers and our paths from site to application are changing. For example, I have a current path of testsite.com/applications/testapp/lowerfolder/lowerfolder/test.aspx?param1=1¶m2=2
I need to have it redirect to testsite.com/testapp/lowerfolder/lowerfolder/test.aspx?param1=1¶m2=2
The parameters and the pages could change along with the two lowerfolders. All I need to do is remove the /applications from the path. Could anyone help with this?
I have BIG-IP version 11.1
Thanks,
Deni
16 Replies
- Kevin_Stewart
Employee
Is "/application" a literal and consistent URI value, or just a representation of multiple possible values? If the former, then a simple string map should suffice:when HTTP_REQUEST { if { [HTTP::uri] starts_with "/application" } { HTTP::uri [string map {"/application/" "/"} [HTTP::uri]] } }
This will transparently change all incoming requests. If you need something more robust, perhaps changing outgoing URIs and links, then I'd suggest either looking at the ProxyPass iRule or the 11.4's new rewrite profile. - djkromarek
Nimbostratus
Below is my iRule, In this particular case, applications is always there, but where I have testapp and beyond that will change. This particular application 'testapp' needs to go to a different pool and have the beginning of the URL change from http://testsite.com/applications/testapp/... to https://testsite.com/testapp/...
The host will always remain the same and in this particular irule, testapp will always remain the same, but what follows after testapp/ will change based on code in the application.
The line HTTP::redirect "https://[HTTP::host][HTTP::uri]" is what I need to change to drop the /application from the URI.
when HTTP_REQUEST {
if { [HTTP::host] contains "testsite.com" } {
if {[string tolower [HTTP::uri]] starts_with "/applications/testapp/" } {
HTTP::redirect "https://[HTTP::host][HTTP::uri]" }
}
} - Kevin_Stewart
Employee
So then:set newpath [HTTP::host][string map {"/applications/" "/"} [HTTP::uri]] HTTP::redirect "https://$newpath" - djkromarek
Nimbostratus
I tried that. My iRule now looks like this:
when HTTP_REQUEST {
if { [HTTP::host] contains "testsite.com" } {
if {[string tolower [HTTP::uri]] starts_with "/applications/testapp/" } {
set newpath [HTTP::host][string map {"/applications/" "/"} [HTTP::uri]]
HTTP::redirect "https://$newpath"
}
}
}
But I am getting a message when testing the site that the redirect is not working correctly. When I click on the link, it doesn't look like it's redirecting at all. The path remains the same as it was originally. But if I use the link that it's trying to redirect and remove the /applications from the url, the page works fine. - Kevin_Stewart
Employee
Can you add a little logging:when HTTP_REQUEST { log local0. "incoming URI: [HTTP::uri]" if { [HTTP::host] contains "testsite.com" } { if {[string tolower [HTTP::uri]] starts_with "/applications/testapp/" } { set newpath [HTTP::host][string map {"/applications/" "/"} [HTTP::uri]] log local0. "redirecting to https://$newpath" HTTP::redirect "https://$newpath" } } } - djkromarek
Nimbostratus
I had forgotten to put in the other posts, that if the request is for application/testapp then I need to go to a different pool, so I added that in, but I still get the same error
when HTTP_REQUEST {
if { [HTTP::host] contains "testsite.com" } {
if {[string tolower [HTTP::uri]] starts_with "/applications/testapp/" }
{
pool "IIS7_Test_Web_Farm"
set newpath [HTTP::host][string map {"/applications/" "/"} [HTTP::uri]]
HTTP::redirect "https://$newpath"
}
}
} - djkromarek
Nimbostratus
I put in the logging and it looked like everything that was under the host testsite.com and within the applications part (not just testapp) went through this rule and logged that it tried to send it to testsite.com/applications/testapp/.... This was every application within the /applications folder not just /testapp. I've had to remove the rule since it seems to want to route everything to the same page. But I would like to try to continue testing somehow to get this to work. - Kevin_Stewart
Employee
A pool will never be touched if you're issuing an immediate redirect. So for example,
1. A user opens a browser and types in "https://www.testsite.com/applications/testapp/lowerfolder/lowerfolder/test.aspx?param1=1¶m2=2".
2. You issue an immediately (302) redirect to "https://www.testsite.com/testapp/lowerfolder/lowerfolder/test.aspx?param1=1¶m2=2"
3. The user returns to "https://www.testsite.com/testapp/lowerfolder/lowerfolder/test.aspx?param1=1¶m2=2"
4. The traffic is sent to a pool based on either configuration or iRule logic. - djkromarek
Nimbostratus
Tried this iRule
when HTTP_REQUEST {
if { [HTTP::host] contains "testsite.com" } {
if {[string tolower [HTTP::uri]] starts_with "/applications/testapp/" } {
set newuripath [string trimleft [HTTP::uri] "/applications"]
HTTP::redirect "http://[HTTP::host]$newuripath"
}
}
}
and this one
when HTTP_REQUEST {
if { [HTTP::host] contains "testsite.com" } {
if {[string tolower [HTTP::uri]] starts_with "/applications/testapp/" } {
set newuripath [string trimleft [HTTP::uri] /applications]
HTTP::redirect "http://[HTTP::host]$newuripath"
}
}
}
Both of them gave me the URL of testsite.comapplications/testapp/.... It took out the /, but didn't remove the applications part. - nitass
Employee
Both of them gave me the URL of testsite.comapplications/testapp/.... It took out the /, but didn't remove the applications part.i think kevin's irule is correct. i do not think trimleft suits what you want.
have you used http analyzer tool such as httpwatch, httpfox? i think it may be helpful in troubleshooting.
HttpFox: The Firefox add-on you can't live without by Lori MacVittie
https://devcentral.f5.com/blogs/us/httpfox-the-firefox-add-on-you-cant-live-without
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