Forum Discussion
Stripping part of URI and integrating with iRule
http://www.oursite.org/directory1/directory2/somepage.aspx needs to rewrite to http://oursite.org/somepage.aspx - essentially just stripping out directory1 and directory2.
I also need to be able to strip the www as well.
I've tried the following iRule that I found as hoolio's example, however it seems to end up getting stuck in a redirect loop.
I also do not want to string map, I need to actually rewrite the URI.
Can anyone tell me what I might be doing wrong? The bigip log shows:
Original URI: /directory1/directory2/somepage.aspx
Current URI: /somepage.aspx
Which seems to be correct, however redirect loop still occurs.
when HTTP_REQUEST {
log local0. "[IP::client_addr]:[TCP::client_port]: Original URI: [HTTP::uri]"
switch -glob [HTTP::path] {
"/directory1/directory2*" {
HTTP::uri [string map {"/directory1/directory2/" "/"} [HTTP::uri]]
HTTP::redirect "http://oursite.org[HTTP::uri]"
}
default {
HTTP::redirect "http://oursite.org"
}
}
}
when HTTP_REQUEST priority 501 {
log local0. "[IP::client_addr]:[TCP::client_port]: Current URI: [HTTP::uri]"
}
I then need to integrate this functionality into the bottom of this iRule, I've stripped most of it as it is very long. The following is at the very end of a large switch statement, and needs to be preserved
default {
if {[HTTP::uri] starts_with "/admin" } {
HTTP::redirect "http://legacy.oursite.org[HTTP::uri]"
} elseif {[string tolower [HTTP::host]] eq "www.oursite.org"}{
HTTP::redirect "http://oursite.org[HTTP::uri]"
}
}
Much thanks for any help!
14 Replies
- Chris_Miller
Altostratus
[HTTP::uri] is a cached item. Even though you've changed it with your string map statement, calling it from the redirect statement won't work because it's value is going to contain /directory1/directory2/.
Using HTTP::uri rewrites the URI between LTM and the pool member. Is that what you're trying to do or are you simply trying to send the user a redirect? - Chris_Miller
Altostratus
[HTTP::uri] is a cached item. Even though you've changed it with your string map statement, calling it from the redirect statement won't work because it's value is going to contain /directory1/directory2/.
Using HTTP::uri rewrites the URI between LTM and the pool member. Is that what you're trying to do or are you simply trying to send the user a redirect? - Joe_Pipitone
Nimbostratus
Yes, I am trying to rewrite from http://oursite.org/directory1/directory2/somepage.aspx to http://oursite.org/somepage.aspx
For some reason there are URL's that are actually http://oursite.org/directory1/directory2/ and still need to go somewhere....
Should I be using HTTP::path? I've tried that also with no luck. Not sure what I am missing. - Joe_Pipitone
Nimbostratus
Well, there's no need to use string map, I just realized that's not what I want as the URI needs to be rewrittenFrom:
http://oursite.org/directory1/directory2/somepage.aspx
to
http://oursite.org/somepage.aspx
I suppose HTTP::path would work too, however I haven't had luck with this iRule, still results in a strange loop:
when HTTP_REQUEST {
if { [HTTP::path] starts_with "/directory1/directory2" } {
HTTP::redirect "http://oursite.org[HTTP::uri]"
log local0. "/directory1/directory2 found, redirected"
} else {
HTTP::redirect "http://oursite.org[HTTP::uri]"
log local0. "go to homepage"
}
}
- Chris_Miller
Altostratus
You're not "rewriting" the URI here. You're redirecting the user to a different one. For your else condition, are you just trying to direct them to their URI but without www in front of the host name? Or are you trying to redirect them to oursite.org/
With the rule above, [HTTP::uri] might contain /directory1/directory2. If it does, your first redirect is to send them to oursite.org/directory1/directory2. Naturally, that's going to be a loop. In the case of your else statement, the same logic applies.
Give this a shotwhen HTTP_REQUEST { if { [HTTP::uri] starts_with "/directory1/directory2" } { HTTP::redirect "http://oursite.org[string range [HTTP::uri] 21 end ]" log local0. "/directory1/directory2 found, redirected" } else { HTTP::redirect "http://oursite.org[HTTP::uri]" log local0. "go to homepage" } } - Joe_Pipitone
Nimbostratus
Hmm....doesn't seem to work, results in a redirect loop as well.From logs:
Rule test HTTP_REQUEST: /directory1/directory2 found, redirectedAnd below that:Rule test HTTP_REQUEST: go to homepagewhat's the reason for the [string range [HTTP::uri] 21 end ]?Also - as far as the else statement, if the user requests /directory1/directory2/somepage.aspx, the somepage.aspx needs to be kept, which I see the HTTP::uri will do for me.Thanks for your help so far. - Chris_Miller
Altostratus
The string range statement grabs the range of characters from the 21st character to the last of the URI. So, it basically ignores the /directory1/directory2/ part and grabs everything after.
We should be able to get rid of the else statement. In the case where /directory1/directory2 doesn't exist, do you still want to get rid of the "www" if the user requested it?
This should cover the first part.when HTTP_REQUEST { if { [HTTP::uri] starts_with "/directory1/directory2" } { HTTP::redirect "http://oursite.org[string range [HTTP::uri] 22 end ]" log local0. "/directory1/directory2 found, redirected" } } - Joe_Pipitone
Nimbostratus
It also appears as if the bigip is trying to send my browser to oursite.org2 - it's placing the number 2 at the end of the url. Strange? - Joe_Pipitone
Nimbostratus
Heya - it worked!I suppose I should have told you that I was masking /directory1/directory2, its actually not 22 chars, but 10 as we're using a 4 letter path
/aaaa/bbbb
so sorry - that's why I was wondering what the number was for.
Thanks for your help, once again very much appreciated!
- Chris_Miller
Altostratus
My pleasure...also, I forgot a "/" so that's why the number 2 was getting placed onto your site name.
Give this a shot. We'll tweak the number of characters for string range to make it work.when HTTP_REQUEST { if { [HTTP::uri] starts_with "/directory1/directory2" } { HTTP::redirect "http://oursite.org/[string range [HTTP::uri] 11 end ]" log local0. "/directory1/directory2 found, redirected" } }
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
