Forum Discussion
Create iRule to change URI Path to parse last directory
Hi,
I'm trying to create an iRule that matches URI starting with “eat9.raspberry.cheesecake.com/cache/_/(_)” to resolves to “eat9.raspberry.cheesecake.com/(*)” via the F5
i.e. “eat9.raspberry.cheesecake.com/cache/1.1.201311042258/js/jquery.js” resolves to “eat9.raspberry.cheesecake.com/js/jquery.js”
Here is the iRule;
when HTTP_REQUEST {
if { [HTTP::host] eq "eat1.blueberry.cheesecakes.com" } {
pool PL_eat1.blueberry.cheesecakes.com_1443
} elseif { [HTTP::host] eq "eat2.blueberry.cheesecakes.com" } {
pool VSEMPINTWEB07_1444
} elseif { [HTTP::host] eq "eat3.blueberry.cheesecakes.com" } {
pool PL_eat3.blueberry.cheesecakes.com_1444
} elseif { [HTTP::host] eq "eat4.blueberry.cheesecakes.com" } {
pool PL_eat4.blueberry.cheesecakes.com_1444
} elseif { [HTTP::host] eq "eat5.blueberry.cheesecakes.com" } {
log local0.alert "hit for eat5.blueberry.cheesecakes.com -> PL_eat5.blueberry.cheesecakes.com_1444"
pool PL_eat5.blueberry.cheesecakes.com_1444
} elseif { [HTTP::host] eq "eat7.blueberry.cheesecakes.com" } {
pool PL_eat7.blueberry.cheesecakes.com_1444
} elseif { [HTTP::host] eq "eat8.blueberry.cheesecakes.com" } {
pool PL_eat8.blueberry.cheesecakes.com_1444
} elseif { [HTTP::host] eq "eat9.blueberry.cheesecakes.com" } {
pool PL_eat9.blueberry.cheesecakes.com_1444
} elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/foneat") } {
pool PL_eat9.blueberry.cheesecakes.com_1444
} elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/css") } {
pool PL_eat9.blueberry.cheesecakes.com_1444
} elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/templates") } {
pool PL_eat9.blueberry.cheesecakes.com_1444
} elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/js") } {
pool PL_eat9.blueberry.cheesecakes.com_1444
} elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/images") } {
pool PL_eat9.blueberry.cheesecakes.com_1444
} elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/cache/*") } {
Get the index of the last URI directory
set depth [URI::path [HTTP::uri] depth]
Parse the last directory in the path
set last_dir [URI::path [HTTP::uri] $depth $depth]
Parse everything after the last directory
set last_string [string trimleft [string range string $last_dir end]]
HTTP::redirect "[HTTP::host]/$last_dir$last_string" }
{ pool PL_eat9.blueberry.cheesecakes.com_1444 }
}
Thanks,
Dave
5 Replies
- Thomas_Gobet
Nimbostratus
Hi,
Could you tell us what's wrong when you use this iRule ?
And last thing, it would be better to use switch instead of multiple if / else if .
- Oxenburger_1420
Nimbostratus
Hi,
I've rewritten the rule using switch -glob as suggested. Not sure if this works yet but will test soon. With /cache/ I'm trying parse the last directory and everything after that. e.g. “eat9.raspberry.pies.com/cache/1.1.201311042258/js/jquery.js” is changed to “eat9.raspberry.pies.com/js/jquery.js” but doesn't seem to be working.
I get error item not found.
elseif { [HTTP::host] eq "eat9.raspberry.pies.com" } { switch -glob [HTTP::uri] { "/fonts/" - "/css/" - "/templates/" - "/js/" - "/images/" - "/pages/" { pool PL_eat9.raspberry.pies.com_1444 } "/cache/" { set depth [URI::path [HTTP::uri] depth] set last_dir [URI::path [HTTP::uri] $depth $depth] set last_string [string trimleft [string range $last_dir [expr {[string last / $last_dir]}] end] -/] [HTTP::uri] "$last_dir$last_string" { pool PL_eat9.raspberry.pies.com_1444 } } }
Thanks
- Thomas_Gobet
Nimbostratus
You don't have to use 2 times $depth for last_dir creation.
You can define it with that :set last_dir [URI::path [HTTP::uri] $depth]
Can you log what your last_string returns to you ?
- Oxenburger_1420
Nimbostratus
Hi,
I couldn't get switch -glob working but will persevere.
I did manage to parse the last 2 directories of the uri + basename + query string. Turns out they wanted the /cache/1.1.201311042258/ stripped from the URI and parse the remaining directory including basename and query string.
e.g eat9.raspberry.pies.com/cache/1.1.201311042258/js/jquery.js” is changed to “eat9.raspberry.pies.com/js/jquery.js”
So this is what seems to work for this part.
elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/cache/") } { set depth [URI::path [HTTP::uri] depth] set last_dir [URI::path [HTTP::uri] 3 $depth] set base [URI::basename [HTTP::uri]] HTTP::uri "$last_dir$base?[HTTP::query]" pool PL_eats9.media.pies.com_1444 }
This is what the entire iRule looks like for now. Or at least until I can retest switch.
when HTTP_REQUEST { { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/API/v") } { pool PL_eats9.pies.com_99 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/APIConfig") } { pool PL_eats9.pies.com_99 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/myGateWay/v") } { pool PL_eats9.pies.com_99 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/testAPI") } { pool PL_eats9.pies.com_99 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/svc/") } { pool PL_eats9.pies.com_99 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/pagedata/") } { pool PL_eats9.pies.com_1443 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/pages") } { pool PL_eats9.media.pies.com_1444 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/fonts") } { pool PL_eats9.media.pies.com_1444 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/css") } { pool PL_eats9.media.pies.com_1444 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/templates") } { pool PL_eats9.media.pies.com_1444 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/js/") } { pool PL_eats9.media.pies.com_1444 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/images") } { pool PL_eats9.media.pies.com_1444 } elseif { ([HTTP::host] eq "eats9.raspberry.pies.com") && ([HTTP::uri] starts_with "/cache/") } { set depth [URI::path [HTTP::uri] depth] set last_dir [URI::path [HTTP::uri] 3 $depth] set base [URI::basename [HTTP::uri]] HTTP::uri "$last_dir$base?[HTTP::query]" pool PL_eats9.media.pies.com_1444 } elseif { [HTTP::host] eq "eats9.raspberry.pies.com" } { pool PL_eats9.pies.com_1443 } }
Thanks,
Dave
- Thomas_Gobet
Nimbostratus
You forgot a if statement at line 2 ;)
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