Redirect from A to B with Persistent Parameters or Values

You can use Monetate's Full-Page Test experiences to redirect from URL A to URL B, while keeping any existing parameters and values intact.

For this use case, site.com/?test=true must redirect to the subdirectory site.com/pathname/?test=true while keeping the test=true parameter intact.

Examples of URLs with a parameter

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

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

In this replacement text example, anything that the regex identifies as the second capture group is preserved and inserted after the replacement text by adding $2 to Replacement Page.

Dissecting the Regular Expression

This URL regex consists of two capture groups.

Diagram of a URL with a bracket setting off the first capture group and another bracket setting off the second capture 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 next piece of the first capture group, (?: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 ?. Finally, it checks if the parameter, if it exists, is followed by a value by looking for any number of any characters with a period and asterisk (.*).

Diagram of URLs with second capture groups

With the second capture group intact, it can then be appended to a new URL in the Full-Page Test experience. On arriving a visitor is instantly redirected by the experience to the new URL with the same parameter in the URL.