A company’s website is kind of like a living entity; over time the CMS might change, pages might move around and even whole sections might get deleted. As part of this change, Url’s for existing page will change. This has happened on pretty much every project that I’ve ever worked on. When it comes to creating re-direct in Umbraco we have several options. In today’s tutorial, I’m going to cover some of the more frequently used techniques to create a re-direct in Umbraco.
Out of the box, Umbraco comes with ‘UrlRewritingNet.UrlRewrite‘. One word of caution before you use UrlRewritingNet.UrlRewrite is that historically, it has had some bad performance issues. I would recommend reading this article before you decide to use it.
If you are only creating one or two redirects and you don’t have a massive amount of traffic, then it is more than fit for purpose. To get started, in your website’s webroot, have a look in ‘Config’ -> ‘UrlRewriting.config’. This is the file that you will use to define the re-direct. Like most re-direct packages, the rules are configured by rules. For example,
<add name="xmlsitemaprewrite" virtualUrl="^~/sitemap.xml" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="/XmlSiteMap" ignoreCase="true" />
The above rule, if you can’t guess, transforms a request from www.website.com/sitemap.xml to XmlSiteMap.
Url Rewrite is probably the most used re-direct tool. It’s built by Microsoft and works directly in IIS. When you add a rule in IIS it adds it directly in the website’s web.config. To get started with Url Rewrite, you need to download it from here. You will need to install it on all of your environments, so dev, test, staging and live.
When the plug-in is installed, if you open IIS you should see the Url Rewrite option
If you click on this in the top right you should see an option called ‘Add Rule(s)..’
When you click on ‘Add Rules’ you should see a screen that gives you a lot of options. In most instances you will just want to create a new inbound blank rule.
The next step in the process is actually defining the rule.
Like UrlRewritingNet.UrlRewrite you can define your Url’s using Regular expression and get as complex as you want to. If, on the other hand, like me, in most cases you just want to create a simple redirect, then the process is pretty easy. Create a name for your rule, this can be anything you want. In the pattern add in the Url, minus the website domain, so www.website.com/sitemap.xml would be sitemap.xml. In the action properties put the Url you want it to re-direct to, in my example I’m redirecting to www.website.com/XmlSitemap which translates to XmlSitemap. Click ‘Apply’ in the top right and job done!
If you look in your website’s web.config you should now be able to see an entry similar to this:
<rewrite> <rules> <rule name="Sitemap Re-direct"> <match url="sitemap.xml" /> <action type="Rewrite" url="XmlSitemap" /> </rule> </rules> </rewrite>
If you prefer to work in your web.config you can just copy the section and change it to meet your needs. Simple!
In today’s tutorial, we’ve covered two methods of creating re-directs within Umbraco. The first is the solution that comes out the box, UrlRewritingNet.UrlRewrite and Microsofts Url Rewrite. I personally wouldn’t recommend using UrlRewritingNet.UrlRewrite. It might come with Umbraco but it’s not as widely used or supported as Url Rewrite. Url Rewrite is used on thousands of servers, can be used in IIS and has good documentation. Url Rewrite is pretty easy to use, install it on all of your servers then either using the GUI or your websites web.config define your rules. Enjoy!