Re: Dynamic Redirects

Anonymous
Not applicable

Dynamic Redirects

Our team is trying to redirect landing pages while keeping the URL parameter data within the querystring. If we are using one landing page for a variety of different networks like, Facebook, Twitter, Website, etc. How would we keep this information through a redirect?

Ex. A person is on Facebook and sees an ad for our content on this landing page:
 
Info.company.com/video?networks=facebook&mostrecentnetwork =facebook
 
And we want the redirect to go to our website:
 
company.com/on-demandvideo
 
We have this landing page on several other sites as well and would like to dynamically populate the parameter info for each one. Do we have to make a redirect for each channel?
Tags (1)
2 REPLIES 2
Kenny_Elkington
Marketo Employee

Re: Dynamic Redirects

Hey Danny,

Check out example 2 here: http://developers.marketo.com/documentation/websites/forms-2-0/

What you'll want to do is grab the querystring before changing location.href like this:

var qs = window.location.href.split("?");

Then append it to your desired location during the redirect:

location.href = "//www.example.com" + qs;

EDIT: See below for a better answer.
SanfordWhiteman
Level 10 - Community Moderator

Re: Dynamic Redirects

@Kenny E if you're trying to keep the QS and pass it verbatim to another page, your approach isn't correct:
  • String::split() returns an array and by defintion does not include the "?" character.
  • It's not necessary to parse location.href at all: location.search already contains the query string.
  • Using scheme-relative URL "//..." means you will never redirect from http:// to https:// or https:// to http://, which is a big assumption.

The correct way is:
 
window.location.href  = "http://redirect.example.com" + window.location.search;

However from the OP's example he isn't using the original QS but rather massaging it into a different hostname and path.  There isn't any magical way to do this except a series of case statements or regexes.