Add a Parameter and Redirect from A to B with Any Previous Parameters or Values Intact

You can use Monetate's Full-Page Test experiences to redirect from URL A to URL B and add a new parameter value while keeping existing parameter value(s) intact.

For this use case, site.com/?test=true should redirect to site.com/?test=true&string=test and site.com should redirect to site.com/?string=text.

URL diagram illustrating 'site.com/?test=true' redirecting to 'site.com/?test=true&string=test'

Within the regular expression (regex) are capture groups that correspond to the Replacement Page field below.

  • $1 is the first capture group.
  • $2 is the second capture group.
  • $3 is the third capture group.

In this example, the regex preserves any parameter value(s) it identifies as the third capture group and then inserts it after the new parameter value to be added by using $3 in the Replacement Page input.

Dissecting the Regular Expression

This URL regex consists of three capture groups and one negative lookahead group.

A URL diagram indicating three capture groups

A negative lookahead only succeeds if the regex inside the lookahead fails to match.

A URL diagram indicating a negative lookahead group

First Capture Group

The initial piece of the first capture group, https?:\/\/, matches for the unsecure (http) and secure (https) versions of the URL protocol for site.com. By including an optional character with s?, it can match for either http or https.

The second piece, (?:www|m|t)\., matches for the subdomain. The vertical pipe (|) character makes this into an array of OR options that matches for www or m, or t for desktop, mobile, or tablet top-level domains.

This example matches all top-level domain variants of the site m.site (mobile) or t.site (tablet), or www.site (desktop). The question mark and colon (?:) set this group to be a noncapture group.

The final piece of the first capture group, site\.com\/, matches for the domain site.com using the backslash escape character (\) to precede any symbols needed as literal characters, such as the forward slash (/) or the period (.).

Second Capture Group

The second capture group, $|\?, checks if the URL either ends by using a dollar sign ($) or looks for a parameter of any kind that's initialized with ?.

Third Capture Group

The third capture group checks if any number of any characters exist after the parameter(s) with .*.

Negative Lookahead Group

The negative lookahead group, ?!.*string=test, looks for what does not match ?! any number of any characters .* followed by a literal string of characters that matches the parameter and value being appended during this Full-Page Test experience string=test.

This piece of the regex is extremely important and ensures a loop occurs of constant redirecting and appending another copy of the same parameter and value.

Illustration showing the loop of constant redirecting and appending

Once the regex is complete, you can see how it works in the Full-Page Test experience. The first part of the URL remains intact and is placed at the beginning of the redirect URL with $1, then the parameter string and value ?string=test& is appended. Including the ampersand (&) is very important because it preserves and appends any additional parameters and values captured in the third capture group that is added next with $3.

The ampersand (&) does not negatively impact the redirect URL if there is no information in third capture group. However, it is present in the URL and therefore impacts any action conditions, such as the URL ends with option.