Forum Discussion
multiple URI redirects
Hello,
I will be migrating a .NET environment with approximately 300 parent web directories. We will need to look at the parent web directory of the application, change the hostname and leave the uri in tact.
EX:
app1
hostname.com/parentdirectory/subdirectory/login.aspx
redirect with 301 to:
newhostname.com/parentdirectory/subdirectory/login.aspx
app2
hostname.com/parentdirectory2/subdirectory/login.aspx
redirect with 301 to:
newhostname.com/parentdirectory2/subdirectory/login.aspx
all the way up to parentdirectory300.
I am trying to not have 200 lines of redirect rules on the F5. Is there a better way of doing this?
Thanks,
Dave
10 Replies
- Kevin_Stewart
Employee
You're going to have to have 200 lines somewhere, but probably better to do it in a data group. So if I understand you, the new hostname is dependent on the requested parent directory, correct? If so, how about something like this:
Data group (example: "my_datagroup")
"/parentdirectory1/" := "hostname1"
"/parentdirectory2/" := "hostname2"
...
"/parentdirectory300/" := "hostname300"
iRule:when HTTP_REQUEST { if { ( [string tolower [HTTP::host]] equals "hostname.com" ) and ( [class match [string tolower [HTTP::uri]] starts_with my_datagroup] ) } { HTTP::redirect "http://[class match -value [string tolower [HTTP::uri]] starts_with my_datagroup][HTTP::uri]" } }
I've also made a giant assumption here that the hostname is the same for all of the old stuff. If not, your data group key might actually be the old hostname and the parent directory.
Example:
"hostname.com/parentdirectory1/" := "newhostname1.com" - Davean_Hosang_1
Nimbostratus
Kevin, thank you! There are some different hostnames so thank you for including that as well. I will rather do the data groups. I am in the network team and the app dev team wanted to provide me with a CSV file with all the redirects and have teh irule reference the File. I would rather the data group...
Thanks again! - Arie
Altostratus
Keep in mind that every time you update a data group (class) the iRule may temporarily fail and/or the values will be unavailable for the duration of the update process...
The outright failure of the iRule can be mitigated relatively easily by verifying that the data class does indeed exist and putting the request on hold, but the temporary unavailability of the entries is more difficult to mitigate.
- Davean_Hosang_1
Nimbostratus
Hello,
I had to create this on a test F5 which is running version 9.4.6 before performing in production. How do I convert the iRule and Data Group from the version 11 format provided above to the version 9.4.6? I noticed that the "class match" was not available on version 9 and we believe that we have to use "matchclass" instead but we recieved the below error.
Error: "line 3: [wrong args] [matchclass -value [string tolower [HTTP::uri]] starts_with dotNet-test]"
Any help would be appreciated.
Previous irule: when HTTP_REQUEST { if { ( [string tolower [HTTP::host]] equals "hostname.com" ) and ( [class match [string tolower [HTTP::uri]] starts_with my_datagroup] ) } { HTTP::redirect "http://[class match -value [string tolower [HTTP::uri]] starts_with my_datagroup][HTTP::uri]" }
- nitass
Employee
Error: "line 3: [wrong args] [matchclass -value [string tolower [HTTP::uri]] starts_with dotNet-test]"
i understand string data group in 9.x does not have "value" pair, does it? anyway, if you want, you may be able to add the value to string and parse it yourself.
sol3386: Overview of data groups
http://support.f5.com/kb/en-us/solutions/public/3000/300/sol3386.html - Kevin_Stewart
Employee
My 9.x coding is a little rusty, but I think it'd go something like this:
Data group (example: "my_datagroup")
"/parentdirectory1/ hostname1" "/parentdirectory2/ hostname2" ... "/parentdirectory300/ hostname300"iRule
when HTTP_REQUEST { if { ( [string tolower [HTTP::host]] equals "hostname.com" ) and ( [matchclass [string tolower [HTTP::uri]] starts_with my_datagroup] ) } { set newhost [findclass [string tolower [HTTP::uri]] my_datagroup " "] HTTP::redirect "http://$newhost[HTTP::uri]" } } - nitass
Employee
shouldn't it be findclass (rather than starts_with matchclass) in if-condition?
e.g.
[root@ve10:Active] config b virtual bar list virtual bar { snat automap pool foo destination 172.28.19.252:80 ip protocol 6 rules myrule profiles { http {} tcp {} } } [root@ve10:Active] config b class my_datagroup list class my_datagroup { { "/parentdirectory1/ hostname1" "/parentdirectory2/ hostname2" } } [root@ve10:Active] config b rule myrule list rule myrule { when HTTP_REQUEST { set newhost [findclass [string tolower [URI::path [HTTP::uri] 1 1]] my_datagroup " "] if { [string tolower [HTTP::host]] equals "hostname.com" and $newhost ne "" } { HTTP::redirect "http://$newhost[HTTP::uri]" } } } [root@ve10:Active] config curl -I http://172.28.19.252/ -H "Host: hostname.com" HTTP/1.1 200 OK Date: Thu, 29 Aug 2013 01:29:46 GMT Server: Apache/2.2.3 (CentOS) Last-Modified: Thu, 23 May 2013 00:28:46 GMT ETag: "4185a8-59-c3efab80" Accept-Ranges: bytes Content-Length: 89 Content-Type: text/html; charset=UTF-8 [root@ve10:Active] config curl -I http://172.28.19.252/parentdirectory1/something -H "Host: hostname.com" HTTP/1.0 302 Found Location: http://hostname1/parentdirectory1/something Server: BigIP Connection: Keep-Alive Content-Length: 0 - Kevin_Stewart
Employee
Pretty sure you could do it either way. Matchclass returns a Boolean and Findclass returns a string.
- Davean_Hosang_1
Nimbostratus
I was able to get Nitass irule to work.
Is there any performance impact on the site when you have 300 lines in a data group? Just trying to get an idea of what to expect when we start to get to the last pages.
- nitass
Employee
Is there any performance impact on the site when you have 300 lines in a data group? Just trying to get an idea of what to expect when we start to get to the last pages.
you may try irule timing to see how many cpu cycle irule utilizes. anyway, if your unit is not under heavy loaded, i think this irule with 300 lines in data group would be pretty fine.
iRules Optimization 101 - 05 - Evaluating iRule Performance by Deb Allen
https://devcentral.f5.com/articles/irules-optimization-101-05-evaluating-irule-performance.Uh62ej_4uCQ
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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