Forum Discussion
kc2kth_49679
Nimbostratus
Feb 06, 2009Rewrite host and URI, then redirect
I thought I was pretty close on this one, but I can't quite get it to work. I need to rewrite a portion of a URI (drop part of the path and change the name of the page), change the host name, then send a redirect back with the new host and uri. It seems I can't use string map to change the path and page name all at once since there is a slash separating the two. I suppose I could use two string maps, but I thought there was probably a better method I'm missing.
Here's the code I've done so far.
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::host]] {
"app.domain.com" {
set host www.google.com
HTTP::uri [string map {"index.jsp" "index.asp"} [HTTP::uri]]
log local0. "redirecting to $host[HTTP::uri]"
HTTP::redirect http://$host[HTTP::uri]
}
}
}
- hoolio
Cirrostratus
That's a good start. You can do pairs of replacements within string map (Click hereπwhen HTTP_REQUEST { switch [string tolower [HTTP::host]] { "app.domain.com" { log local0. "redirecting to http://www.google.com[string map -nocase {"/path_to_replace" "/new_path" "index.jsp" "index.asp"} [HTTP::uri]]" HTTP::redirect "http://www.google.com[string map -nocase {"/path_to_replace" "/new_path" "index.jsp" "index.asp"} [HTTP::uri]]" } } }
- kc2kth_49679
Nimbostratus
when HTTP_REQUEST { switch [string tolower [HTTP::host]] { "app.domain.com" { set host "www.google.com" set uri [string map -nocase {"/path_to_replace" "/new_path" "index.jsp" "index.asp"} [HTTP::uri]] HTTP::redirect http://$host$uri } } }
- hoolio
Cirrostratus
Of course, you're right. Sorry about that. I edited the example above to skip setting the URI with HTTP::uri. - kc2kth_49679
Nimbostratus
So my requirements have changed a bit. I have the code below working. However what we now need to do is NOT send a redirect to the client and instead rewrite the uri portion of the request and send that to an existing server pool.when HTTP_REQUEST { switch [string tolower [HTTP::host]] { "app.domain.com" { set host "www.google.com" set uri [string map -nocase {"/path_to_replace" "/new_path" "index.jsp" "index.asp"} [HTTP::uri]] HTTP::redirect http://$host$uri } } }
- kc2kth_49679
Nimbostratus
Replying to myself here since I did some more work on this, but still don't have it working the way I was hoping. Basically we want to try and avoid a large number of redirects that would occur with our original solution and instead update the uri and select a pool for the connection. Poking around here I found some examples that used HTTP::header replace, but I haven't had much luck. - hoolio
Cirrostratus
You're very close. To update the URI without a redirect you can use HTTP::uri. You'd use HTTP::header replace for the Host header because for whatever reason, you cannot rewrite the host using HTTP::host "newhost".when HTTP_REQUEST { switch [string tolower [HTTP::host]] { "host.domain.com" { HTTP::uri [string map {"/string" "/newstring" "page.jsp" "newpage.aspx"} [HTTP::uri]] pool pool_name } default { reject } } }
when HTTP_REQUEST { log local0. "Priority 500: [IP::client_addr]:[TCP::client_port]: Original request to [HTTP::host][HTTP::uri]" switch [string tolower [HTTP::host]] { "host.domain.com" { HTTP::uri [string map {"/string" "/newstring" "page.jsp" "newpage.aspx"} [HTTP::uri]] pool pool_name } default { reject } } } when HTTP_REQUEST priority 501 { log local0. "Priority 501: [IP::client_addr]:[TCP::client_port]: Updated request to [HTTP::host][HTTP::uri]" }
- kc2kth_49679
Nimbostratus
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