Forum Discussion

Brandon_11574's avatar
Brandon_11574
Icon for Nimbostratus rankNimbostratus
Mar 27, 2011

Special 404 Redirect Rules

We have some sub folders in our site that we want special 404 handling. If there are any 404 errors on any file found under that folder or any of its children folders, we want to employ this 404 handling. This can easily be done with something like the following:

 

 

if { ([HTTP::status] contains "404") or ([HTTP::status] contains "400")} {

 

if { [HTTP::uri] starts_with "/$subFolder/" } {

 

HTTP::redirect http://domain.com/$subFolder/specialPage.aspx

 

}

 

}

 

 

But here's my dilemma. I have over 500 different sub folders that need to have this rule applied to. Is there a way to store the sub folder list into an array or similar type variable? We would like to simply maintain the 'array' and not worry about writing a large number of if statements. Or does anyone have a good way for us to comply with this 'requirement' with reasonably maintainable code?

 

 

  • Hi Brandon,

     

    Here is the following link to using data groups to accomplish what you are requesting

     

     

    Here is the link

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/findclass.html

     

     

    Write up an iRule and see how it goes. If you run into issues then please post up the iRule that you have written and hopefully we can guide to your final solution.

     

     

    I hope this helps

     

    Bhattman

     

  • Sure. You can store them in a Data Group.

    (Local Traffic -> iRules -> Data Group List).

    You can store them as strings and search through them with a class match.

    Try something like this:

    
    when HTTP_REQUEST {
    set targethost [HTTP::host]
    set targeturi [HTTP::uri]
    }
    when HTTP_RESPONSE {
    if { [HTTP::status] == 400 || [HTTP::status] == 404 } {
    if { [class match $targeturi starts_with mydatagroup ] } {
    HTTP::redirect "http://$targethost[URI::path $targeturi 1 1]specialPage.aspx"
    }
    }
    }
    
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    The class match example is where I'd go with this too. Keep in mind that if you want even more customization you could store the actual location of the specialized error pages in the class as well (since classes support key/value pairs) and pull them out for each request, allowing for more granular control.

     

     

    Colin