Skip to main content

Sitecore SXA wild card redirect from language code url to non language code url

Hi Folks

This will be a quick post as i wanted to quickly share this scenario and i am sure it will be helpful for many of you as it includes SEO inputs and how we had to change few things to support it

 


Scenario

Recently we changed the implementation of our sitemap generator as it was not respecting the "language embedding" flag, which is a known issue mentioned here , so we had to overwrite it and make our own to make it work, but all good so far so now our site map did not have any URLs with language code appended to it.

But our SEO team came up with the analysis that there are users still going to 404 page because of they are hitting URL with the language code, Although we had that flag to true in Sitecore.config settings


So, Logically all pages should be accessible only without language code in the URL

Now, we knew that if we hit pages with language code they are going to 404, but our SEO team insisted that it should go to the actual pages with 301 redirect, and it should happen site wide because of either old URL book marked or search engines might have references of it and if someone is coming from there.

Solution

Well, SXA redirect module is there from the inception but till now i only did one to one mapping of OLD to NEW url, but in this case we wanted to identify the language code (we only had one language i,e "en") in the incoming url and wanted to redirect users to non language specific URLs and that is where i started playing around with wild card pattern in the SXA redirect module.

Question i had was i will be able to write the pattern for incoming URL but how can i redirect user to pages without the language code? that is where TOKEN came into picture 

SXA redirect module provides tokens like $1, $2 and so on which will be replaced by corresponding strings from the input regex

so now it was time to test things my left side regex was like below (or refer the screenshot on the top to see how mapping was done to achieve this)

^/en(/.*)$

and gave its corresponding URL on right hand side as "/$1"

that means, redirect all incoming request with language code to the corresponding page without the language code

I tested and it worked like a charm !!! and now we did not have any users landing to error page if they hit pages with language code in the URL.

I hope this will help someone looking for something like this.

Comments

Popular posts from this blog

401.1 Unauthorized with windows authentication error code 0xc000006d

How many of you have faced this hosting issue when you do everything what it takes to run the site with windows authentication but still you are getting the same error again and again? If you think you also have faced the same issue and you tired of reading MSDN KBs for it and still have not found the issue (If KB has solved the issue, well and good, if not you can try this trick),Please Read below Typical scenario In typical hosting with IIS, i did every possible things like enabling windows authentication, changing it in web.config, configuring connection pool, authorization rules, it asks me for window authentication login and despite of entering correct credentials it always fails and keeps on asking for login, and when pressed cancel it gives 401.1 with 0xc000006d error code Solution (Which worked for me at-least after trying for almost 6-9 hrs) You need to change the Loop Back Check in registry so that it allows the host names which you are giving in url are allowed and au...

Set up leprechaun code generation with Sitecore XM Cloud Starterkit

Hi Sitecorians, It has been amazing learning year so far and with the change in technology and shift of the focus on frontend frameworks and composable products, it has been market demand to keep learning and exploring new things. Reasons behind this blog Today's topic is something that was in my draft from April-May, and I always thought that there is already a good documentation out there for  Leprechaun  and a blog post is not needed, Until I realized that there was so many of us facing same kind of issues and same kind of problems and spending same amount of time, That is where I thought, if I could write something which can reduce that repetitive troubleshooting time, That would really help the community. 1)  In a project environment, if we get into some configuration issues, we resolve them, we make sure we are not blocked and continue, but if you think same issue, same step and same scenario will come to other people, so if we can draft it online, it will help othe...

Error in Sitecore 10.3 XM with CDs - Could not find configuration node: database/database[@id='master']

Hi Team, Recently we came across two distinct scenarios with same error message.  1) We started getting below error when we try to hit our layout service API endpoint. If we hit end point of CM, it was working fine and when we try to hit end point of CD it was giving this error, It was clear there is something in backend expecting master DB instead of WEB DB In first scenario my colleague  Jatin  also got same issue while setting up docker instance for 10.3, and my friend  Akshay Barve  and I had the same observation. 2) Second scenario I was having the same exception, It was another project on 10.3 XM with CD, It was one of the migration project so we were migrating from 10.2 to 10.3, Older site was not giving any error but new site the moment we hit, it was giving same exception in the log Issue in second scenario was in site grouping there was only site and both CM and CD URLs were given in host name and had WEB db as database, so basically there was no previ...