Sitecore SXA has a really nice feature of redirecting traffic from old pages to new pages (new URLS). It has (as all SXA features) really great documentation on how to set it up. In combination with Sitecore JSS (Integrated mode), you can use it also on your Sitecore JSS projects.

Setting up Direct Redirects

Under your Site Settings, you can find Redirects items. Just create a new Redirects Map item:

Setting it up is straight forward:

On the left side you have old URL path and on left side you are specifying new URL path. One of the HTTP Begin pipelines is then checking whether URL matches the left part and if so redirects user to URL on right side based on Type of redirection selected in upper field (Type of redirect).

There can be three types of redirections:

  • Permanent Redirect (HTTP 301) – redirects target resource to a permanently different URL.
  • Temporary Redirect (HTTP 302) – redirects target resource to a temporarily different URL.
  • Server Transfer – helps reduce server requests, keeps the URL the same and is not visible to the client. For example, when you want to transfer the current page request to another .aspx page on the same server or when you want to avoid unnecessary roundtrips to the server.

Setting up Redirects with Regular Expressions (Regex)

This is more or less the same as above mentioned Direct Redirect but the Regular Expressions you want need to be starting with ^ and ending with $.

As documentation states – For example, the regular expression ^/ab[cd]/$ matches the following URLs:

  • /abc/
  • /abd/
  • /abc
  • /abd

Setting up sophisticated Redirects with Regular Expressions (Regex)

We had a requirement to support redirects using regular expressions, where part of the old URL would be taken and used in new URL.

So something like /pages/* to /newpages/*  e.g. /pages/support/contact-us will redirect to /newpages/support/contact-us.

Documentation states this:

  • Regular expression – if the pattern starts with ^ (match only if the following is at the beginning of the line) and ends with $ (match only if the previous is at the end of the line) regular expression matching is used. The target can include tokens ($1, $2, and so on) that will be replaced by corresponding strings from the input. Patterns must include / at the beginning and the end. For example: ^/blogs/blogs-november/$

I have highlighted the important part that will help us achieve reqs.

Unfortunately, there is no example of using $1, $2 tokens in documentation hence this blog post :-).

We have tried several combinations to fulfil requirements but this one won:

Old value – ^/pages(.*)/$
New value – ^/newpages$1

So basically what this combination does is that it tries to match all the old URLs that start with /pages , takes what is afterwards that is a regex (.*) [“take everything”] and places it instead of $1 token in new URL after /newpages. You can place several regexes in old URL and then use $1 $2 tokens in your new URLs.

So exactly as we wanted – old URL /pages/support/contact-us will redirect visitor to /newpages/support/contact-us at the end.

Happy redirecting… 🙂