Forum Discussion

kevinmc's avatar
kevinmc
Icon for Altocumulus rankAltocumulus
Jun 30, 2017

URL rewrite

I am trying to rewrite a URL for users who are using an old URL.

 

So if the user enters sharepoint.abc.net I want to redirect them to home.sharepoint.abc.net

 

I have tried an irule as below

 

if {[string tolower [HTTP::Host]] starts_with "sharepoint.abc.net"} { HTTP::header replace Host "home.sharepoint.hscni.net" }

 

But it doesn't work. Can someone give the correct code or is there another way to do it?

 

3 Replies

  • Hi kevinmc,

    first you need to be clear about the wording rewrite and redirect.

    Redirect: Sending the client a 301/302 response with the new correct URL. The client will then send a new request towards the new URL.

    Rewrite: F5 takes the URL from the client, changes the URL (hostname and/or URI) and forward the request with this new URL towards the real server. Means the client still sees the "old" URL in its browser bar, but the real server sees the new URL.

    So you are talking about a redirect and this can be handled with the following iRule:

    when HTTP_REQUEST {
        if {[string tolower [HTTP::Host]] starts_with "sharepoint.abc.net"} {
             For a 301 redirect us this line
            HTTP::respond 301 location "http(s)://home.sharepoint.hscni.net[HTTP::uri]"
             For a 302 redirect us this line
            HTTP::redirect "http(s)://home.sharepoint.hscni.net[HTTP::uri]"
        }
    }
    

    Hope that's clear now.

    Ciao Stefan 🙂

  • I'm faced with similar issue however I want to do a URL rewrite and foward to different back pool. For example, if client points to http(s)://sharepoint/blog/** , i want to rewrite the host and URI to http(s)://blog.com/** (changing the host header + removing the /blog and retaining the URI). How would I go about doing that?

     

  • Hi Hongster,

    please try this iRule (not tested):

    when HTTP_REQUEST {
        if {[string tolower [HTTP::Host]] equals "sharepoint" and [string tolower [HTTP::uri]] starts_with "/blog/"} {
             Change Host-Header
            HTTP::header replace Location "blog.com"
             Cut the first characters (based on your needs) from the URI
            HTTP::uri [substr [HTTP::uri] 5]
        }
    }
    

    Depending on your required logic the rule might be optimized, but it should give you an idea what you need to do here.

    Ciao Stefan 🙂