cancel
Showing results for 
Search instead for 
Did you mean: 

Conditional redirect using trafficscript

SOLVED
Highlighted
New Contributor

Conditional redirect using trafficscript

Hi All,

Recently a customer of ours utilized a redirect script to good effect, but it doesn't tick all the boxes on a new requirement they have.

I have tried in my limited non-programmer way to figure out the best way to achieve the new requirement with no real luck and thought I would ask in here.

Essentially the problem is this - the customer uses whitelabelling on a given Virtual Server, and wants a specific dynamic redirect to happen - namely that if the full URL is blog.foo.bar/<whatever> they want this rewritten to foo.bar/blog/<whatever>. The domain can and will be different all the time.

This isnt as easy as it seems from what I can tell, as it needs the host header rewritten, and then the url path conditionally rewritten to add the /blog/ in front of the <whatever>

Does anyone have any suggestions on where to start?

Regards,


Adam

2 REPLIES
Contributor

Re: Conditional redirect using trafficscript

Hi Adam,

If I've understood your issue correctly, then the below TrafficScript should work. Given a list of hostnames, we change the host to be some other host and optionally add a start path to the one provided.


$hosts = [ "blog.foo.com" => "www.foo.com/blog",


           "images.foo.com" => "www.foo.com" ];



$host = http.getHostHeader();



if ( hash.contains($hosts, $host) ) {


   if ( string.regexMatch($hosts[$host], "([^/]*)(/.*)") ) {


      $path = http.getPath();  


      http.setHeader("Host", $1);


      http.setPath($2 . $path);


   } else {


      http.setHeader("Host", $hosts[$host]);


   }


}


This does a rewrite of the request, so it is transparent to the browser. If you prefered you could instead issue a redirect. All you need to do is change the setheader() to be a http.redirect() with the full path, eg http.redirect("http://" . $hosts[$host]).

Cheers,

Mark

Re: Conditional redirect using trafficscript

Hi,

I am using the same traffic script as Adam has specified and I would like to have the following adjustments:

When a URL rule has upper case characters as a requirement, using the same URL with lower case does not work eg:

foo.bar/Contact      works

foo.bar/contact      does not work

The second adjustment is that some of the URL's when redirected to the home page eg:

foo.bar/info to foo.bar

It gets the error - Bad Request - Invalid URL HTTP Error 400 the Request URL is invalid.

Could you please advise of an adjustment to the previously specified rule that can be used to counter these?