Forum Discussion

kgco's avatar
kgco
Icon for Nimbostratus rankNimbostratus
Feb 13, 2020

100s of redirects in policies. How do I get them from 302 to 301

Hello all,

 

I am working on LTM 11.4 soon to upgrade to 12.1 or 13. I have 100's of redirect policies. They test out as 302 redirects. How do I get them to redirect as 301 permanent redirects.

  • Hi,

    If you move to v14, you have the option to set the code in the redirect rule.

    In earlier versions, I used to set a tcl variable on policy rule and read then in iRule script.

    I don't have a v11.4 screenshot, but it's close to this:

    The iRule content:

     

    when HTTP_REQUEST {
        if { [info exists redirect301] } {
            HTTP::respond 301 -version auto noserver Location $redirect301 Connection close
            unset redirect301
        }
    }

     

    If you decide to change it before move to v14, to replace hundred of rules, you can list policies and run a script or manually edit then to merge it on command line:

    e.g.

     

    tmsh list ltm policy my_policy  |awk '{ sub(/http-reply$/,"tcl") sub(/redirect$/,"set-variable name redirect301") sub(/location(.*)$/,"expression " $2) }1'

     

    Original content / before to be edited:

     

    ltm policy my_policy {
        controls { forwarding }
        requires { http }
        rules {
            rule1 {
                actions {
                    0 {
                        http-reply
                        redirect
                        location https://www.mydomain.net[HTTP::uri]
                    }
                }
                ordinal 1
            }
        }
        strategy first-match
    }

     

    After run command / edit:

     

    ltm policy my_policy {
        controls { forwarding }
        requires { http }
        rules {
            rule1 {
                actions {
                    0 {
                        tcl
                        set-variable name redirect301
                        expression https://www.mydomain.net[HTTP::uri]
                    }
                }
                ordinal 1
            }
        }
        strategy first-match
    }

     

    You must check if all rules are right, OK? 🙂

    Then, you can save it on file (e.g. myfile.conf) to merge on command line or read from terminal.

    See:

    https://support.f5.com/csp/article/K81271448

    Make sure it's all OK with "verify" statement:

    e.g.

    # tmsh load sys config merge file /var/tmp/myfile.conf verify

    When all it's OK, merge it:

    # tmsh load sys config merge file /var/tmp/myfile.conf

    Please, run a backup before make changes.

    I hope it helps.

  • Hi,

    If you move to v14, you have the option to set the code in the redirect rule.

    In earlier versions, I used to set a tcl variable on policy rule and read then in iRule script.

    I don't have a v11.4 screenshot, but it's close to this:

    The iRule content:

     

    when HTTP_REQUEST {
        if { [info exists redirect301] } {
            HTTP::respond 301 -version auto noserver Location $redirect301 Connection close
            unset redirect301
        }
    }

     

    If you decide to change it before move to v14, to replace hundred of rules, you can list policies and run a script or manually edit then to merge it on command line:

    e.g.

     

    tmsh list ltm policy my_policy  |awk '{ sub(/http-reply$/,"tcl") sub(/redirect$/,"set-variable name redirect301") sub(/location(.*)$/,"expression " $2) }1'

     

    Original content / before to be edited:

     

    ltm policy my_policy {
        controls { forwarding }
        requires { http }
        rules {
            rule1 {
                actions {
                    0 {
                        http-reply
                        redirect
                        location https://www.mydomain.net[HTTP::uri]
                    }
                }
                ordinal 1
            }
        }
        strategy first-match
    }

     

    After run command / edit:

     

    ltm policy my_policy {
        controls { forwarding }
        requires { http }
        rules {
            rule1 {
                actions {
                    0 {
                        tcl
                        set-variable name redirect301
                        expression https://www.mydomain.net[HTTP::uri]
                    }
                }
                ordinal 1
            }
        }
        strategy first-match
    }

     

    You must check if all rules are right, OK? 🙂

    Then, you can save it on file (e.g. myfile.conf) to merge on command line or read from terminal.

    See:

    https://support.f5.com/csp/article/K81271448

    Make sure it's all OK with "verify" statement:

    e.g.

    # tmsh load sys config merge file /var/tmp/myfile.conf verify

    When all it's OK, merge it:

    # tmsh load sys config merge file /var/tmp/myfile.conf

    Please, run a backup before make changes.

    I hope it helps.

    • kgco's avatar
      kgco
      Icon for Nimbostratus rankNimbostratus

      You, my fine man, are a godsend. Thank you so much Claudio!

      • cjunior's avatar
        cjunior
        Icon for Nacreous rankNacreous

        wow, nice to hear it. When confirm it works, please mark this question as solved.

        Cheers