Forum Discussion

John_Eapen_1372's avatar
John_Eapen_1372
Icon for Nimbostratus rankNimbostratus
Nov 06, 2013

Can we perform URL rewrite based on the header in the request using iRule or any other way?

For Example suppose a request www.abc.com with a header wherein language is German is received, can we rewrite the URL such that it becomes www.abc.com/ge and serves a German web page instead of the default English page...

 

1 Reply

  • Absolutely. Something like this:

    when HTTP_REQUEST {
        switch [HTTP::header Accept-Language] {
            "en" {
                HTTP::uri "/en[HTTP::uri]"
            }
            "ge" {
                HTTP::uri "/ge[HTTP::uri]"
            }
        }
    }
    

    This will look at the incoming Accept-Language header and insert the respective language path before the original request. So a request to www.abc.com/foo/bar with an Accept-Language value of "ge" will produce a request to the server as www.abc.com/ge/foo/bar.

    Now the above should work as long as the server doesn't return relative links to its resources under this path. Example:

    /en/image.jpg
    

    If it does do this, then you'll need a minor modification of the above rule to catch this and disregard URI modification if the language path already exists:

    when HTTP_REQUEST {
        switch [HTTP::header Accept-Language] {
            "en" {
                if { not ( [HTTP::uri] starts_with "/en/" ) } {
                    HTTP::uri "/en[HTTP::uri]"
                }
            }
            "ge" {
                if { not ( [HTTP::uri] starts_with "/ge/" ) } {
                    HTTP::uri "/ge[HTTP::uri]"
                }
            }
        }
    }
    

    The HTTP::uri command will modify the URI as it passes through the proxy, and will be transparent to the user (won't show up in the address bar).