cancel
Showing results for 
Search instead for 
Did you mean: 

How to redirect Domains/Url with Trafficscript

SOLVED
Highlighted
Occasional Contributor

How to redirect Domains/Url with Trafficscript


Hi @ all,

I´ve once again a short question ...

I try to redirect all of our reserved Domains to our Main-Domain and I´ll do this with TS.

It should be regardless if the Domains are called with 'www' or without 'www' and if something is behind the '/'  or not ...

an example:

Domain "mycompany.com" and "www.company.com" (also mycompany.com/* and www.mycompany.com/* )

should be redirected to "https://www.myfavoritedomain.com"

Here´s my TS-Rule, at the moment every Domain has it´s own line (without and with 'www' )

but how can I realize as short as possible (because we have many of them), that this Domains when called with "www" will also be redirected ?


$headerhost = http.getheader( "host" );



if( $headerhost == "mycompany.com"


     || $headerhost == www.mycompany.com


     || $headerhost == "mysecondcompany.com”


     || $headerhost == "www.mysecondcompany.com"


|| $headerhost == "mythirdcompany.com"


|| $headerhost == "www.mythirdcompany.com"


|| $headerhost == "myfourthcompany.com"


|| $headerhost == www.myfourthcompany.com)


{


  http.changeSite( "https://www.myfavoritecompany.com"/ );  }


 











The Rule have to be with Syntax "$headerhost == " instead of "$headerhost contains " because some of the Domainnames are used as a Subdomain ...and these shouldn´t be redirected of course ...

Perhaps somebody have a hint for me?

Regrads,

Michael

1 REPLY
Occasional Contributor

Re: How to redirect Domains/Url with Trafficscript

We have a similar situation.  Rather than use a big list of conditions chained together with II we use a resource file.  It's syntactically prettier, but also you could update the list without having to rewrite the TrafficScript.  We update ours with the REST API.  Here's what you might try (NOT TESTED!):


# Make a resource file called domains.txt and put it in your catalogs > extra files


# > miscellaneous files.  Each line in the text file has a domain name that you


# want to match to redirect.


$resource = 'domains.txt';


# make sure the file is present before you attempt to parse it.


if(! (resource.exists($resource))){


  # uncomment this line if you want to log when it's missing.


  # log.warn("Resource file " . $resource . " not found!");


  break;


}


$hostHeader = http.getHostHeader();


$domains = resource.getLines($resource);


foreach ($line in $domains){


  $domain = $string.trim($line);


  if ($hostHeader == $domain){


     http.changeSite("http://myfavoritedomain.com/");


  }


}





Now, you could use a string.regexmatch to check a domain entry with the host header, but be careful!  You may accidentally match domains too broadly.