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!
- Chris_MillerAltostratus[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/.
- Chris_MillerAltostratus[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/.
- Joe_PipitoneNimbostratusYes, I am trying to rewrite from http://oursite.org/directory1/directory2/somepage.aspx to http://oursite.org/somepage.aspx
- Joe_PipitoneNimbostratusWell, there's no need to use string map, I just realized that's not what I want as the URI needs to be rewritten
From:
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_MillerAltostratusYou'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/
when 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_PipitoneNimbostratusHmm....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_MillerAltostratusThe 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.
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_PipitoneNimbostratusIt 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_PipitoneNimbostratusHeya - 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_MillerAltostratusMy pleasure...also, I forgot a "/" so that's why the number 2 was getting placed onto your site name.
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