Forum Discussion
Dave_Horsey_225
Nimbostratus
Mar 10, 2006Rewrite URL and change pool trouble
I have been working on a irule to rewrite the URL to do the following:
User sends http://www.sitename.com/blog
bigip changes the url to http://blog.sitename.com/ and redirects the request to a different pool
when HTTP_REQUEST {
if { [HTTP::uri] contains "www.sitename.com/blog" } {
HTTP::redirect "http://blog.sitename.com/"
}
if {[HTTP::uri] contains "www.sitename.com/blog" }{
pool default
} else {
pool different
}}
16 Replies
- First thing that sticks out is that the your are combining the host and uri in your "contains" comparison. Also, a redirect sends control back to the browser with a redirect code specifing the new url. Your pool statements after the redirect are not valid because control is never not passed to the backend server due to the redirect. Then the browser will come in with the new uri and it won't match your comparison.
when HTTP_REQUEST { if { "[HTTP::host][HTTP::uri]" starts_with "www.sitename.com/blog" } { HTTP::redirect "http://blog.sitename.com" } }
So all requests that start with "http://www.sitename.com/blog" will be sent back a response to the browser to make a new connection to "http://blog.sitename.com".
If you want to do multiple pools because both hostnames are on the same virutal, then you can use the pool statement to override the default pool but make sure you are comparing the correct strings.
-Joe - Dave_Horsey_225
Nimbostratus
Is it possible to rewrite the URI instead of redirect? (ie. the browser thinks that it is still going to www.sitename.com/blog for the entire session)
Should I just setup a seperate iRule to handle the pool redirect function? - Sure is. Check out this blog post on that subject.
http://devcentral.f5.com/weblogs/joe/archive/2005/07/27/ModifyingUriWithoutRedirect.aspx
Click here
-Joe - Oops, forgot your second question. If you are just rewriting the URI, I'd include it all in a single rule.
-Joe - Dave_Horsey_225
Nimbostratus
Here is what I have right now....
when HTTP_REQUEST {
if { [HTTP::uri] starts_with "www.xxxxxxxxxinsight.com/blog" } {
HTTP::uri "cs.geoxxxx.com/blogs/menopauseinsightblog [string range $uri 41 end]"
}
if {[HTTP::uri] contains "/blog" }{
pool MIS-WEB21-CS
} else {
pool MIS-WEB20-4
}}
The irule is redirecting the request, but I don't the the uri is getting changed? The MIS-WEB21-CS member server reports that /blog is being requested and gives a 404. I am not sure if I have the string range correct? - A couple of things.
1) The URI does not contain the hostname so your "if" is probably not passing.
1) The URI cannot have spaces. You are inserting a space before your "string range" command.
2) What is the value of $uri? I don't see it defined anywhere.
3) Have you added any logging to verify that your "if" condition is met? My guess is that it is not. it should be something like this:if { [HTTP::uri] starts_with "/blog" } { ... }
If you need to verify the host as well, you'll have to "and" two comparisons together using the HTTP::host value.
Adding simple logging is always your best debugging step to sort out issues like this.
-Joe - Dave_Horsey_225
Nimbostratus
Here is my next attempt, but this won't allow me to save the iRule due to my then and else statements?
What do you mean by define uri$? Isn't defined using the starts_with statement?
when HTTP_REQUEST {
if { [HTTP::uri] starts_with "/blog" } {
HTTP::uri "/blogs/menopauseinsightblog[string range $uri 27 end]"
}
then { [HTTP::header host] eq "www.xxxxxxxxxinsight.com" } {
HTTP::header replace "cs.geoxxxx.com"
else { [HTTP::header host] eq "xxxxxxxxxinsight.com" } {
HTTP::header replace "cs.geosign.com"
}
}
if {[HTTP::uri] contains "/blog" }{
pool MIS-WEB21-CS
} else {
pool MIS-WEB20-4
}} - Dave_Horsey_225
Nimbostratus
To clarify, I only want the host to be changed if it matches the /blog uri string. - In your HTTP::uri command, you are referencing a variable named "uri". This will be undefined unless you specifically set it with the "set" command. Also, the error for "then" is because there is no "then" reserved word. The "then" is the contents of the enclosed braces.
Also, are you sure the "string range" command is returning what you want. It's returning from the 27th character to the end of the string, or an empty string if the URI isn't 27 characters.
Give this a try.when HTTP_REQUEST { if { [HTTP::uri] starts_with "/blog" } { HTTP::uri "/blogs/menopauseinsightblog[string range [HTTP::uri] 27 end]" } if { [HTTP::host] eq "www.xxxxxxxxxinsight.com" } { HTTP::header replace "Host" "cs.geoxxxx.com" } elseif { [HTTP::host] eq "xxxxxxxxxinsight.com" } { HTTP::header replace "Host" "cs.geosign.com" } if { [HTTP::uri] contains "/blog" } { pool MIS-WEB21-CS } else { pool MIS-WEB20-4 } }
-Joe - Dave_Horsey_225
Nimbostratus
I get the following iRule error....
01070151:3: Rule [Blog_Redirect_test] error:
line 9: [deprecated usage, use else or elseif] [ ]
line 9: [missing an expression] [ ]
line 5: [undefined procedure: [HTTP::uri] contains "/blog" ] [if { [HTTP::host] eq "www.xxxxxxxxxinsight.com" } {
HTTP::header replace "Host" "cs.geoxxxx.com"
} elseif { [HTTP::host] eq "xxxxxxxxxinsight.com" } {
HTTP::header replace "Host" "cs.geoxxxx.com"
} if { [HTTP::uri] contains "/blog" } {
pool MIS-WEB21-CS
} else {
pool MIS-WEB20-4
}]
I am just trying to replace /blog with /blogs/menopauseinsightblog I though the string had to be the length of the uri replacement string. Also, there will be other pararmeters after the string that I don't want replaced.
i.e. /blog?name=star would turn into /blogs/menopauseinsightblog?name=star
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
