Forum Discussion

norrtom_376408's avatar
norrtom_376408
Icon for Nimbostratus rankNimbostratus
Nov 21, 2018

Can someone help me with iRule redirects?

Hi All!

I am new to F5 and iRules. I'm helping out a client of mine to do redirections.

I understand that if I want to redirect several URL's from an old domain to a new one, i will do as this:

 when HTTP_REQUEST { 
 if { [string tolower [HTTP::host]] eq "www.old-example.com" and [HTTP::path] eq "/my-old-path/page-y" } {
   HTTP::respond 301 Location "https://www.new-example.com/new-path/page-x"
 }
       if { [string tolower [HTTP::host]] eq "www.old-example.com" and [HTTP::path] eq "/my-old-path-2/page-zz" } {
   HTTP::respond 301 Location "https://www.new-example.com/new-path-2/page-zzy"
 }
             if { [string tolower [HTTP::host]] eq "www.old-example.com" and [HTTP::path] eq "/my-old-path-n/old-page-n" } {
   HTTP::respond 301 Location "https://www.new-example.com/new-path-n/new-page-n"
 }   }

Can some of you experts out there confirm that the rules above are correct for simple redirections?

Since I will also need to do wildcard redirections as in redirected to , can you help me out with a basic redirect rule for regex redirections?

The wildcard redirections will always have some numbers in the end (page) that change, but else it will always be a quite static slug. Can you help me?

Thank you so much for your time!

Br, Tom

1 Reply

  • Hi Tom,

    Your iRule looks ok and would do the job but there's a bit of repetition. I would check the host header first and do it once, then use a switch statement for all your static redirects. As for your 'wildcard' redirect, you can extract the numbers using

    getfield
    and simply add it as a variable to the end of your redirect.

    when HTTP_REQUEST {
        set newHost "www.new-example.com"
        if {[string tolower [HTTP::host]] eq "www.old-example.com"} {
           switch -glob [string tolower [HTTP::uri]] {
               "/my-old-path/page-y" {
                   HTTP::respond 301 Location "https://${newHost}/new-path/page-x"
                }
               "/my-old-path-2/page-zz" {
                   HTTP::respond 301 Location "https://${newHost}/new-path-2/page-zzy"
               }
               "/my-old-path-n/old-page-n" {
                   HTTP::respond 301 Location "https://${newHost}/new-path-n/new-page-n"
               }
               "/the/old/slug/*" {
                    set numbers [getfield [HTTP::uri] / 5]
                    HTTP::respond 301 Location "https://${newHost}/my/new/url/$numbers"
               }
           }
        }
    }
    

    Let me know how you get on.

    Lee