An improved 301 Redirect Module for Sitecore
If you maintain a website you have almost certainly needed to move pages around. Maybe your site got restructured, some section of your site was renamed, or a shorter URL is required for an ad campaign. You need to make sure existing links to your site continue to work, or you’ll lose out in search engine rankings and frustrate users who’ve bookmarked your site. The main tool available to website administrators is the redirect, but Sitecore doesn’t manage redirects out of the box.
Sitecore users have a few options to keep links working.
- Redirection before the request reaches Sitecore, such as with IIS URL Rewrite
- Clone items, which avoid redirecting but clutter the site tree.
- A home-grown solution: usually a Sitecore Pipeline to intercept requests and redirect if they match certain rules.
- The Sitecore Shared Source 301 Redirect Module. This uses items to specify what redirects are in place, so content editors can add, edit and remove redirect rules without developers getting involved – the holy grail of content management.
At Igloo we were using a home-grown solution as described, but were running into challenges. We looked at the Shared Source 301 Redirect Module to see if it could help.
The redirect module already had a couple of great, unique features:
- Redirects are items. They can be put through workflows, published, unpublished.. – but most importantly, they can be seen and altered by content editors – the holy grail of content management.
- Regular expression support in the redirect module gives a lot of flexibility – for instance, you can redirect all URLs under an area of your site to another place.
- Although the redirect module runs on every request to your site, if the URL is valid (ie. it matches an item in Sitecore), the redirect module does nothing. This keeps your site nice and fast.
- It is URL based rather than template based, so any item can be fetched without needing to know its template in advance.
Our requirements were:
- Let content editors manage redirects.
- Don’t clutter the site’s item tree with items whose only purpose is redirecting.
- Ability to redirect part of a query string, so we could, for example, configure requests to /basketball?colour=pink to redirect to /basketball?colour=orange.
- Redirect for some URLs some of the time, even if the URL is correct.
- Minimum impact on overall performance of the site.
So we enhanced the redirect module to meet our requirements. Now we’re contributing our work back to Shared Source for the benefit of the community! Here’s what we’ve added:
- The ability to work with Sitecore friendly URLs:
If a user requests http://mysite.com/mypage and you configure a redirect to http://mysite.com/my-renamed-page, the module correctly maps the redirected request to a Sitecore item called “My Renamed Page”. (In other words, the enhancement supports Sitecore’s ability to map spaces in Sitecore items’ names to hyphens in URLs and vice versa, and deal with URL-encoded spaces. The original module doesn’t do this.)
- Case insensitivity when matching URLs. A request to http://mysite.com/SomePage will be treated the same as a request to http://mysite.com/somepage.
- The redirect module normally only works on URLs which don’t exist in Sitecore; this is for performance reasons. With this enhancement you can specify a (small) set of items be specified for which redirects will still be processed:
- Ability to use regular expressions to control what portion of the query string, if any, is included when the redirect is issued. You can use this to add, change or remove parts of the query string when redirecting. Combined with the above feature this makes the redirect module very powerful and flexible. Here’s an example (more examples are available below if you click on the GitHub link):