Design & Technology June 22, 2015
How to turn Sitecore 8 aliases into redirects
Redirects are a common feature that our clients request for many reasons, such as their URL structure changing or SEO requirements. Redirects are extremely handy to have, and even better when a non-technical user can create them without having to rely on IIS modifications.
Sitecore has a few options in this situation, for example, I’m a big fan of the Sitecore URL Rewrite Module by Andy Cohen. If you haven’t checked it out already, it provides a lot of the standard IIS rewrite module functionality, but through Sitecore instead.
Another way of implementing redirects if you don’t want something as heavy duty as the URL Rewrite module, is by over-riding aliases in Sitecore. You can provide redirects to the user and at the same time, restrict a feature of Sitecore that can be misused.
Often people don’t realise the SEO implications of having two URLs for the same page, which is exactly what aliases do. While you could avoid a situation like this by implementing a canonical URL, as recommended by Google, in my opinion it’s much better to simply hijack this functionality and use it to provide redirects instead.
So, what to do?
Well, if we’re going to hijack aliases, you’d expect us to have to write huge chunks of code and override all kinds of classes, right?
Nope! All we have to do is write a single pipeline modification that comes in at just 60 lines of pretty simple code.
Without further ado, here’s all you need:
(CODE IS UNAVAILABLE, CAN BE FOUND HERE
After that, you just need a way of replacing out the alias resolving pipeline with our new version. Since we know what we’re doing, let’s add this as an include and patch out the original pipeline, like so:
CODE IS UNAVAILABLE, CAN BE FOUND HERE
Of course, don’t forget to swap out the namespace for your own instead of ‘launch Sitecore’. Once that’s done, it’s pretty simple to see the results; open your network tab and you’ll see that your aliases have turned to redirects.