lchln_304378
Dec 29, 2016Nimbostratus
Translate mod_rewrite to iRules
Hi guys,
Trying to migrate mod_rewrite rules to F5 iRules and am not having much success. The current setup uses a regex to get the subdomain from a HTTP_HOST and then rewrites the URL, appending the subdomain to the URI. The regex is used as we serve multiple clients and use sub-domains to distinguish them.
eg.
abc.domain.com rewrites to domain.com/abc
mod_rewrite rules here:
RewriteCond %{HTTP_HOST} ^([^.]+)\.abc\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !pages/ [NC]
RewriteCond %{REQUEST_URI} !test_pages/ [NC]
RewriteCond %{REQUEST_URI} !testpages/ [NC]
RewriteRule ^(.*)$ https://abc.domain.com/test//%1/%{REQUEST_URI} [P,L]
I've written up the below and have tried to implement but with little success:
when HTTP_REQUEST {
if {[string tolower [HTTP::host]] matches_regex {^([^.]+)\.abc\.domain\.com$}} then
{
HTTP::redirect "https://abc.domain.com/test//%1/%[HTTP::host]"
}
}
Any advice? Am I misusing HTTP::host and should I instead be using HTTP:uri? I was messing around with using a string map but I don't have enough knowledge to make it work:
when HTTP_REQUEST {
set subdomain [string map {".abc.domain.com" "" "abc.domain.com" ""} [string tolower [HTTP::host]]]
if { $subdomain ne "" } {
HTTP::redirect "https://abc.domain.com/test//%1/$subdomain"
}
}
Thanks in advance for any advice.