Forum Discussion

Paul_Jenkins_12's avatar
Paul_Jenkins_12
Icon for Nimbostratus rankNimbostratus
Nov 19, 2013

Lots of specific URLs to 301 redirect - best approach?

I have a lot of specific URLs that are currently returning 404s that I need to 301 redirect to specific new locations (about 300+). The data is in an excel file with URL404 and URLNew as the columns - is there an easy way to use a Data Group and an iRule to handle these if I can get the pairs into the Data Group somehow? I'll continue to look as this is likely a common thing, but everything I've been finding tonight is about pattern matching/etc and my problem is literally a large number of specific 1-1 301s that are needed and I only really want to write one iRule and handle the 1-1s via something easily managed / modified.

 

Thanks! Paul

 

4 Replies

  • This should be really easy, something like this;

    when HTTP_REQUEST {
     Is this one of the 'bad' URIs?
     if { [class match [string tolower [HTTP::uri]] equals dg_name_of_choice] } {
      It is, let's redirect
      HTTP::redirect [class match -value -- [string tolower [HTTP::uri]] equals dg_name_of_choice] }
    }
    
  • Paul,

     

    Yes, a data group and short iRule would be perfect for this.

     

    Create a string data group, for the purposes of this example, called datagroup_redirects. The String would be the url you want to match on and the Value would be the url you want to redirect to. So the entries would look something like...

     

    www.domain.com/if/this/is/requested := http://www.domain.com/redirect/to/here

    The code to perform the redirect would be something like...

     

    when HTTP_REQUEST {
        if { [class lookup [HTTP::host][HTTP::uri] datagroup_redirects] ne "" } {
            HTTP::respond 301 Location [class lookup [HTTP::host][HTTP::uri] [datagroup_redirects]
        }
    }

    Attach the iRule to the appropriate Virtual Server.

     

  • Hi Paul,

    if you have the list already as an Excelsheet, then a Data Group List should be your best friend.

    Is the hostname of your 404-URLs always the same, means do you have just 300+ different URIs or do you have several different DNS-names pointing to the same VIP?

    In Excel you have to put together your first two columns with the following command (in C1):

    =CONCATENATE("""";A1;" ";B1;"""")

    Then create a DGL from type string and leave it empty.

    Connect via SSH to your BIG-IP and edit your conf-file:

    pico bigip.conf

    Search for your created DGL by pressing Strg+W and enter "class [name of your DGL]" (without quotes). You should find something like this (sorry can't use curly brace):

    class [name of your DGL] (

    type string

    )

    Replace it with (sorry can't use curly brace): class [name of your DGL] (

    (

    [insert here your block from column C from your Excelsheet]

    )

    )

    Save your changes with Strg+O and exit with Strg+X.

    Verify your changes with (v10.x):

    b verify load

    And activate it with (v10.x):

    b load

    Assuming in column A is just the URI and in column B the redirect URL like in below example:

    A1=/path1/file.html

    B1=http://new.site.com/redirect

    you can create an iRule similar like this (v10.x, sorry can't use curly brace):

    when HTTP_REQUEST (

    set redirect_url [findclass [HTTP::uri] [name of your DGL] " "]

    if ( $redirect_url != "" ) (

    HTTP::respond 301 Location "$redirect_url"

    )

    )

    You can also work with external files as Data Group List, but I didn't have the correct iRule commands in mind right now (same as for v11.x as findclass is not valid there anymore).

    This is also not tested, but should point you in the right direction.

    Ciao Stefan 🙂

  • Yes, it's just a A->B huge list of direct URLs, all from the same base URI (http://www.blah.com/thisUriIs404 needs to 301 to http://www.blah.com/muchmorebetterUri)

     

    Thanks everyone will be trying these approaches out today and will report back!