Redirection in SXA

301 redirect. That's something we use a lot on websites when there is a change in some part of URL. If not, your old URL or bookmark throws 404. So, to achieve the redirection, we can use regex in config file and redirection happens and gives a valid page with 200 response. All good.

What if you are using a SXA site and want to do redirections but don't want to make changes to the config because it needs to go through your CI/CD pipeline and its time-taking process. So, SXA comes with an inbuilt redirection module. There are various types of redirections.

1-1 redirection

If you have an item in your content tree and the name of that item is changed. So, you want to make a redirection. You can do this by simply adding an item of template: /sitecore/templates/Feature/Experience Accelerator/Redirects/Redirect . All you have to do is to add an item of this template to your content tree where your old item is present and name it with the old item name. So your request reaches this item and then in the item you can give a reference to the new item where it should be redirected to. This will do the redirection without any further steps. That's it! 

Redirection using Redirection Map

Another way of doing a redirection is using a redirect map which is in site settings. Here, in the redirection map you can do multiple ways of redirection. 

1-1 Redirection

Here you'll have a text field which says old URL to new URL you are mapping, so you can simply give  the old in the left-hand side of the text field and new on the right-hand side. This also works like the 1-1 redirection which we discussed earlier. Instead of doing it at a content tree, you can do that 1-1 redirection in redirect map. This is another option.

Redirection Using Regex

You can use regular expressions also to do the redirection. The regular expression can be used in such a way that you can change a part of the URL and you can still make the URLs work, like if you have a parent page name that got changed and you have a set of sub pages underneath the parent. Using the regular expression, you can make sure all the references to that URL's not break and to redirect to new rename URL. It will not break any subpages also, so this can be done using tokens. Please see the example below.


In this above image, the first example shows the 1-1 redirection using a redirect map. This is the simple way of doing the 1-1 redirection. You can add as many redirections as you want. Example 2, you can see the redirection using a regular expression. This will work for the page the redirectest and also any subpages that are under the redirectest. If you see on the right-hand side, there is $1, $2 which are acting as tokens and they will take care of that extra text that is before the keyword and after the keyword. 

Hope this helps.

Happy Redirecting!!

Comments

Popular posts from this blog

Xhtml Validation Error on Experience editor