Sitecore tips and tricks and community news !

Post Top Ad

Post Top Ad

Thursday, March 16, 2017

Redirects 1/2



Once has to come a day when someone ask you to prepare Redirects in Sitecore. For instance you are working on some old site migration. You want to build new really cool structure so it means you will loose old content tree structure probably. Hence we will destroy SEO on which client has been working for months...

And here redirects come.


1. Why redirects in Sitecore ?

Because we can manage it ! We can create separate item which map single redirect and setup any settings that we will provide by ourselves. Additionally if we have a lot of redirects to create we can build importer from *.csv file for instance.

2. How it works ?

We are creating Sitecore items representation which will map our redirects. After that we can create custom code that will read this items during processing http request and decide to make redirect or not. To do that we create our custom processor that we will inherit from HttpRequestProcessor, and it will be added as a patch to config part like:

To help imagine that we can draw diagram

In this picture we can see how flow of http request goes through Sitecore. It processes in our custom http processor and as a result Sitecore change response url or not.

3. How can look sample use case ?

Everybody know if something looks understandable, then details could be more problematic. So, lets try to check how it will be in the field.

I would like to add that I'm omitting things like "how to create redirect template", because I'm sure all of you know what is item data template and how to create it :)

If we would like to have useful approach we should to distinguish between two kinds of subtrees.

a) Redirect type - which will describe status of particular redirect
b) Redirect item - which will define single redirect

Picture below describe this:

Ok, but what fields we can have in those items ? In our example we can have:

Redirect type fields
Redirect item fields









As you can see, old address field is marked on red, what means is validated as required field. I think is useful details, because who wants to create redirect without filling old address value?

After we have done redirect items, custom code left for us. At the point "2. How it works" I've mentioned about custom http processor, which will interpret http request and check is particular request target url exist in redirects list. If yes, Sitecore has to change url to new redirect value.

We have to stop here for a while and think a little bit about list of redirects. Because we have to check this list each time we will process http request, have to ask, is a good idea to collect this list each time from a scratch ?

Answer is:
- Definitely no :)

We should prepare some mechanism that will keep it in the memory and return value. Because list of redirects is common for whole application, we can clean up our studies memories and try to do Singleton object, which is common for whole application and it has only one instance. Hence, we will avoid situation to create multiple instance of the same list. Sounds great, doesn't it ? :) 


When we have our list of redirect, lets make redirect handler:

I hope you don't mind that I will avoid describing each of the method inside. But we have to notice one action: SyncRedirectsWithStore .Somebody can ask, "Yo dude, what is going on here ? ". Fortunately we know that our list is Singleton instance and sometimes we will have situations when we will have to sync. In this place I realized that post is really long, so rest information I will back to the new one.

As the summary for our place in the redirect's space we know:
- when can do redirects in Sitecore
- what is the flow of http request
- which items we need in Sitecore to serve redirects
- which processor will be responsible for redirects in Sitecore

I hope that information will be helpful for you, feel free to contact with me on socials or email ;)

All the bests,
Robert !

No comments:

Post a Comment