Forum Discussion

whootang's avatar
whootang
Icon for Nimbostratus rankNimbostratus
Nov 03, 2019

irule to redirect and rewrite url

Hi guys,

I'm playing around with irules and im trying to do some smarts around a base url redirect and then a rewrite on sub folders.

So if you hit testdata.com it will redirect you to xyz.com

but if you hit testdata.com/data/* it will rewrite to abc.com/data/*

i cant seem to get it working. any pointers will be awesome.

when HTTP_REQUEST {
    if { [string tolower [HTTP::uri]] starts_with "/" } {
        HTTP::redirect "https://xyz.com/"
    }
    if { [string tolower [HTTP::uri]] starts_with "/data/*" } {
        HTTP::redirect "http://abc.com/data/*"
    }
}

  • Hi whootang,

    If you use starts_with for "/", second "if" will never work.

    when HTTP_REQUEST {
    	if { [HTTP::uri] equals "/" } {
    		HTTP::redirect "https://xyz.com/"
    	}
    	if { [string tolower [HTTP::uri]] starts_with "/data/" } {
    		HTTP::redirect "http://abc.com[HTTP::uri]"
    	}
    }

    Alternative:

    when HTTP_REQUEST {
    	if { [HTTP::host] equals "testdata.com" } {
    		switch -glob [string tolower [HTTP::uri]] {
    			"/" { HTTP::redirect "https://xyz.com/" }
    			"/data/*" { HTTP::redirect "http://abc.com[HTTP::uri]" }
    		}
    	}
    }
  • Great eaa let me test this bad boy out, thanks for taking the time to reply :)