Forum Discussion
Brantly_Perry_1
Nimbostratus
Nov 12, 2008Need HTTP Redirect assistance
Hey everyone. Need assistance figuring out the best way to accomplish a redirect.
http://host.com/uri
needs to redirect to
http://host.com/member?cid=uri
So uri can be anything.
Also, would it be best to integrate this into the existing iRule shown below? How's the best way to do that?
when HTTP_REQUEST {
switch [HTTP::path] {
/ { HTTP::path "/member" }
/bp { HTTP::path "/member?cid=bp" }
/tooljournals { HTTP::path "/tooljournals" }
/eb-member-earAdmin { HTTP::respond 404 content "RESOURCE NOT FOUND" }
/admin { HTTP::respond 404 content "RESOURCE NOT FOUND" }
/adminTool { HTTP::respond 404 content "RESOURCE NOT FOUND" }
}
}
Thanks so much for any input!
11 Replies
- hoolio
Cirrostratus
Hi there,
Do you want to redirect the client (so the browser address bar shows the update) or do you want to rewrite the URI and send the request to the pool?
I'm not sure under what circumstances you want to rewrite or redirect to /member?cid=uri, but here are two methods:
Redirect
HTTP::redirect "http://[HTTP::host]/member?cid=[HTTP::uri]
Rewrite URI
HTTP::uri "/member?cid=[HTTP::uri]"
Also, I think you'd want to replace HTTP::path with HTTP::uri for this line in your sample above:
/bp { HTTP::path "/member?cid=bp" }
HTTP::path does not include the query string. So I would think you'd run into a problem using HTTP::path to set a path that includes a query string if the original request already had a query string.
Original URI: /test/file.ext?param1=param2
Command: HTTP::path /member?cid=bp
Resulting URI?? /member?cid=bp?param1=param2
Aaron - Brantly_Perry_1
Nimbostratus
Thanks a lot for the input! I believe you have gotten me very close, however; it's not quite right. The uri rewrite is not changing, however when I replace HTTP::uri with bp it works fine. Here's what I have:
when HTTP_REQUEST {
switch [HTTP::path] {
/ { HTTP::path "/member" }
/tooljournals { HTTP::path "/tooljournals" }
/HTTP::uri { HTTP::uri "/member?cid=[HTTP::uri]" }
/eb-member-earAdmin { HTTP::respond 404 content "RESOURCE NOT FOUND" }
/admin { HTTP::respond 404 content "RESOURCE NOT FOUND" }
/adminTool { HTTP::respond 404 content "RESOURCE NOT FOUND" }
}
}
Appreciate your help. - hoolio
Cirrostratus
What are the symptoms of the problem you're seeing with the last rule? Which requests are not being rewritten correctly? What are they being rewritten to?
I think this might be what you're looking for:when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: New request to : [HTTP::uri]" switch [HTTP::path] { "/" { HTTP::path "/member" } "/bp" { HTTP::uri "/member?cid=[HTTP::uri]" } "/eb-member-earAdmin" - "/admin" - "/adminTool" { HTTP::respond 404 content "\ RESOURCE NOT FOUND" } } } when HTTP_REQUEST priority 501 { Log a debug statement in a rule event with a different priority so the HTTP::uri value isn't cached. This event can be removed after testing. log local0. "[IP::client_addr]:[TCP::client_port]: Modified URI: [HTTP::uri]" }
What were you trying to do with the case below? It's rewriting the path to the same as it was when requested:
"/tooljournals" { HTTP::path "/tooljournals" }
Also, if you want to use the same action for multiple cases, you can add them with hyphens as shown above for /eb-member-earAdmin, /admin and /adminTool.
Aaron - Brantly_Perry_1
Nimbostratus
Thanks a lot for your input, Aaron. The only thing is that the "/bp" can be anything. Whatever that
value is, I need to rewrite it in the uri.
"/bp" { HTTP::uri "/member?cid=[HTTP::uri]" }
With "/toolsjournals" I need any uri that starts with this, to rewrite the uri as is.
My thinking was that if I didn't do this, then the condition above would catch it, and the uri
would rewrite like this. "/member?cid=tooljournals"
"/tooljournals" { HTTP::path "/tooljournals" }
This make more sense?
starts_with "/tooljournals" { HTTP::uri "[HTTP::uri]" }
Thanks so much! - hoolio
Cirrostratus
How about this then?when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: New request to : [HTTP::uri]" switch -glob [HTTP::path] { "/" { HTTP::path "/member" } "/tooljournals*" { do nothing } "/eb-member-earAdmin" - "/admin" - "/adminTool" { HTTP::respond 404 content "\ RESOURCE NOT FOUND" } default { HTTP::uri "/member?cid=[HTTP::uri]" } } } when HTTP_REQUEST priority 501 { Log a debug statement in a rule event with a different priority so the HTTP::uri value isn't cached. This event can be removed after testing. log local0. "[IP::client_addr]:[TCP::client_port]: Modified URI: [HTTP::uri]" }
Aaron - hoolio
Cirrostratus
Ah, I think you'd need to not rewrite if the path already starts with /member/. Can you try this:when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: New request to : [HTTP::uri]" switch -glob [HTTP::path] { "/" { HTTP::path "/member" } "/member/*" - "/tooljournals*" { do nothing } "/eb-member-earAdmin" - "/admin" - "/adminTool" { HTTP::respond 404 content "\ RESOURCE NOT FOUND" } default { HTTP::uri "/member/?cid=[HTTP::uri]" } } } when HTTP_REQUEST priority 501 { Log a debug statement in a rule event with a different priority so the HTTP::uri value isn't cached. This event can be removed after testing. log local0. "[IP::client_addr]:[TCP::client_port]: Modified URI: [HTTP::uri]" }
Aaron - Brantly_Perry_1
Nimbostratus
Thanks for all the help. It's working almost perfectly now. Only issue that I'm working on now is an unexpected "/" after the rewrite.
So we are seeing:
https://abcwww.mynurseonline.com/member/?cid=/bc
We'd need to see:
https://abcwww.mynurseonline.com/member/?cid=bc
Thanks,
B - hoolio
Cirrostratus
The URI will always start with a leading forward slash. If you need to remove this when using the URI in the redirect, you can use 'string trimleft' (Click here😞when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: New request to : [HTTP::uri]" switch -glob [HTTP::path] { "/" { HTTP::path "/member" } "/member/*" - "/tooljournals*" { do nothing } "/eb-member-earAdmin" - "/admin" - "/adminTool" { HTTP::respond 404 content "\ RESOURCE NOT FOUND" } default { HTTP::uri "/member/?cid=[string trimleft [HTTP::uri] /]" } } } when HTTP_REQUEST priority 501 { Log a debug statement in a rule event with a different priority so the HTTP::uri value isn't cached. This event can be removed after testing. log local0. "[IP::client_addr]:[TCP::client_port]: Modified URI: [HTTP::uri]" }
Aaron - Brantly_Perry_1
Nimbostratus
Looks like it is dropping everything after the first slash. In other works it looks like this.
https://abcwww.mynurseonline.com/bp
Thanks again. - hoolio
Cirrostratus
string trimleft should work:
% set uri /test/1/2/3/
/test/1/2/3/
% puts "/member/?cid=[string trimleft $uri /]"
/member/?cid=test/1/2/3/
Can you post a sample or two of the logs from it not working?
Thanks,
Aaron
Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects
