Technical Forum
Ask questions. Discover Answers.
cancel
Showing results for 
Search instead for 
Did you mean: 

iRule to redirect a URL but replace only a portion of the variable length URI??

Kalibre46_28860
Nimbostratus
Nimbostratus

Hi,

 

I'll start by saying I'm new to iRules but have been a Networking professional for many years, so please go gently on the code newbie!

 

There is a redirect irule that I need to create that I will eventually want to also make a call to a data group, however for the purposes of this question I just want to learn some syntax without the data group.

 

I have a client that is migrating sharepoint data to a newer server and wishes to redirect old user links to the new server. An example of the structure of the original URL is as follows:

 

http://oldsharepoint.uk/a/123/documents/doc1.doc

 

and the new URL to be redirect to will be:

 

http://newsharepoint.uk/new/documents/doc1.doc

 

There are over 3000 iterations of the "/a/123" portion and they are variable in length so might be "/b/1/" or "/a/1234/". I wish to match the first portion of the URI (? sorry my terminology may be off) and replace it with an equivalent.

 

The reason I will use a data group eventually is that these 3000 are being migrated one at a time over a period of months so I will add a container URL to the data group each time one is migrated. However the catch is that I must retain the last part of the path, so "/documents/doc1.doc" in the example above.

 

I'm not sure how I can match and swap out a portion of the variable length URI whilst retaining the last part!?!?

 

Any ideas?

 

Thanks.

 

2 REPLIES 2

R_Marc
Nimbostratus
Nimbostratus

regsub would be one way to do that.

regsub -nocase -all {^/[a-z1-9]+/[a-z1-9]+/documents} [HTTP::uri] "/new/documents" uri

Then just set HTTP::uri to $uri.

That regsub would capture the scenarios you listed.

cjunior
Nacreous
Nacreous

Hi, I don't know if I clearly understood, so I would try another way:

1) Single string pattern for documents path (no regsub)

when HTTP_REQUEST {
    if { [string tolower [getfield [HTTP::host] ":" 1]] equals "oldsharepoint.uk" } {
        if { [string match -nocase "/*/*/documents/*" [HTTP::path]] } {
            set uri [string range [HTTP::uri] [string first /documents [string tolower [HTTP::uri]]] end]
            HTTP::respond 301 noserver Location "http://newsharepoint.uk/new${uri}"
            unset uri
        }
    }
}

2) data group rewrite (I'd recommend for large list):

ltm data-group internal dg_redirect_shpt_paths {
    records {
        /a/1234/ {
            data /new/
        }
        /b/1/ {
            data /new2/
        }
    }
    type string
}
when HTTP_REQUEST {
    if { [string tolower [getfield [HTTP::host] ":" 1]] equals "oldsharepoint.uk" } {
        set p [URI::path [string tolower [HTTP::uri]] 1 2]
        set m [class match -value $p equals dg_redirect_shpt_paths]
        if { $m ne "" } {
            HTTP::respond 301 noserver Location "http://newsharepoint.uk[string map -nocase "$p $m" [HTTP::uri]]"
        }
        unset -nocomplain p m
    }
}

3) rewrite with array list:

when CLIENT_ACCEPTED {
     I don't like using statics variables, so here it is and not in RULE_INIT
    array set redirect_shpt_paths {
        /a/1234/    /new/
        /b/1/       /new2/
    }
}
when HTTP_REQUEST {
    if { [string tolower [getfield [HTTP::host] ":" 1]] equals "oldsharepoint.uk" } {
        set p [URI::path [string tolower [HTTP::uri]] 1 2]
        foreach m [array names redirect_shpt_paths] {
            if { $m eq $p } {
                set uri [string map -nocase "$p $redirect_shpt_paths($m)" [HTTP::uri]]
                HTTP::respond 301 noserver Location "http://newsharepoint.uk$uri"
            }
        }
        unset -nocomplain p m uri
    }
}
when CLIENT_CLOSED {
    unset -nocomplain redirect_shpt_paths
}

If I'm wrong, I apologize,

Respectfully.