For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

M_Housley_11941's avatar
M_Housley_11941
Icon for Nimbostratus rankNimbostratus
Nov 22, 2013

Irule - change 302 redirect iRule to a 301 redirect

I have the following 302 redirect iRule running, but i need to change it to a 301 redirect. I have a potential solution - would someone be able to verify it for me?

Existing rule: -

when HTTP_REQUEST {  
     set host [HTTP::host]  
     if { ($host ends_with "aaa.com") or  
             ($host ends_with "bbb.com") or  
             ($host ends_with "ccc.com") } {  
           set path [HTTP::path]  
           if { ($path starts_with "/one/") or  
                     ($path starts_with "/two/") } {  
                set newuri "https://$host[HTTP::uri]"  
                HTTP::redirect $newuri
           }  
     }  
}

New Rule: -

when HTTP_REQUEST {
     set host [HTTP::host]
     if { ($host ends_with "aaa.com") or
             ($host ends_with "bbb.com") or
             ($host ends_with "ccc.com") } {
           set path [HTTP::path]
           if { ($path starts_with "/one/") or
                     ($path starts_with "/two/") } {
                set newuri "https://$host[HTTP::uri]"
                **_HTTP::respond 301 $newuri_**
           }
     }
}

1 Reply

  • John_Alam_45640's avatar
    John_Alam_45640
    Historic F5 Account

    The HTTP::redirect expects only the destination URL and it constructs a redirect for you.

     

    So, for the HTTP::respond, you have to specify the "Location" header which normally contains the destination URL.

     

    Here is how you do it:

     

    HTTP::respond 301 Location $newuri

     

    more examples here: https://devcentral.f5.com/wiki/iRules.HTTP__respond.ashx