Handling dot(.) in 301 URLs with SXA Redirects

Recently for one of our project we were adding 301 Redirects in Sitecore that is based on SXA, created SXA Redirect map Item based on “Redirect Map” template and starting adding the URLs(old and new URLs mapping).

What we observed that 50% of the URLs had dot (.) in it with specific extension (for example- .oldextension), so when we tested that entry that didn’t worked as expected because of Sitecore not processing that specific extension.

The same scenario has been listed here as well- https://sitecore.stackexchange.com/questions/2413/301redirectmodule-isnt-working-for-url-with-dot-file-name

So- what’s the solution? how to handle dot (.) in the old URL and redirect to new URL using SXA?

There are two options we have:

Define static rules using IIS URL Rewrite module

In this case what we can do is- create different rules for handling different URLs and redirect to the target URLs directly from IIS, assuming we have the following URL mapping:

Old URL- http://site.local/category.oldextension and New URLhttp://site.local/categorylanding– we can use the following IIS rule to handle the redirect.

<rule name=”301RedirectRule1″ stopProcessing=”true”>
<match url=”^(category.oldextension)$” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^site.local$” />
</conditions>
<action type=”Redirect” url=”http://site.local/categorylanding&#8221; />
</rule>

RedirectRule1
In the above case we won’t be using SXA Redirects to handle 301 redirects, how about when we have several hundreds of items? I think it doesn’t make sense to keep adding all the static URLs to IIS, so to support this we can go for second solution which is listed below.

Add the specific rule to remove the dot from the URL

In this approach we write an IIS rule to remove the dot from requested Old URL, for e.g. we change http://site.local/category.oldextension to http://site.local/categoryoldextension

<rule name=”Remove Dot For Redirect” stopProcessing=”true”>
<match url=”^(.*[^.])\.oldextension+$” />
<action type=”Redirect” url=”{R:1}oldextension” />
</rule>

RedirectRule2

Once we have the above rule set- we can just add the above URL(without dot in it) in SXA redirect and then the redirect should work as expected.

Advantage of above approach is that we just have to set one rule and all the URLs are managed directly in Sitecore and content editors can make the change as and when required.

SXARedirect

Hope this helps!

Happy learning 🙂

 

SXA redirect URL with country code

It is important to redirect user to new page from old page when we create new version of an existing web page, it helps both users and search engines to get redirected to a new URL from the one they originally requested.

sxa-redirects

There are several ways to setup redirects in Sitecore solution, with Sitecore SXA we have redirect tool in place that facilitates setting up old and new URLs in Sitecore. We can use the mapping tool to set up a 301/302 or server transfer redirect.

Recently we worked on setting up redirects using SXA redirect tool, for most of the redirects we had country code in the old URL.

Here are some example URLs:

Old URLen-au/sample-old-page-name   New URL– /new-page-name

Old URLen-au/mypage/pagelisting   New URL– /newpagelisting

What we observed that after setting up old URLs which has country code on it- all such URLs were redirecting to 404 page.

We can fix this by appending “sc_lang” querystring parameter to the new URL with the target country code, see below examples for ref:

Old URL– /sample-old-page-name   New URL– /new-page-name/?sc_lang=en-AU

Old URL– /mypage/pagelisting   New URL– /newpagelisting/?sc_lang=en-AU

After this redirects works as expected.

I hope this helps.

Happy Learning 🙂