When moving a client from an old site to a new one, you frequently need to redirect Legacy URLs to new ones to maintain SEO. IIS 7 Rewrite Rules with Rewrite Maps easily accomplish this. A Rewrite Map is simply a subset of a rule with a list of source/destinations. With a bit of massaging, you can take a Excel workbook and export to a map.
The only problem with IIS rewrites are they are added to the web.config file, which for a Sitecore app is already 4000+ lines. Here’s how to keep it clean:
Add the section everything inclusive of the <rewrite> tags just before the ending </system.webServer> tag.
<system.webServer> {lots of stuff here} <rewrite> <rewriteMaps configSource="RewriteMaps.config" /> <rules configSource="RewriteRules.config" /> </rewrite> </system.webServer>
Add two files RewriteMaps.config and RewriteRules.config (samples below) to the web root. IIS will save any changes and additions to the external config files instead of the web.config.
In my starter files (below) are two rewrite maps, Legacy URLs and Marketing URL’s. Legacy would be for the old to new pages which will likely never change, and Marketing rules is for advertising campaigns etc. Keeping them segregated makes updates easier.
RewriteMaps.config
<rewriteMaps> <rewriteMap name="Legacy URLS"> <add key="/about_directory.asp" value="/About/Find-an-Office.aspx" /> <add key="foo" value="bar" /> </rewriteMap> <rewriteMap name="Marketing Rules"> <add key="/jobs" value="/AboutUs/Careers.aspx" /> </rewriteMap> </rewriteMaps>
RewriteRules.config
<rules> <rule name="Redirect rule1 for Legacy URLS"> <match url=".*" /> <conditions> <add input="{Legacy URLS:{REQUEST_URI}}" pattern="(.+)" /> </conditions> <action type="Redirect" url="{C:1}" appendQueryString="false" /> </rule> <rule name="Rewrite rule1 for Marketing Rules"> <match url=".*" /> <conditions> <add input="{Marketing Rules:{REQUEST_URI}}" pattern="(.+)" /> </conditions> <action type="Rewrite" url="{C:1}" appendQueryString="false" /> </rule> </rules>
One thing to note with this: If you make changes to either of these files manually (in a text editor) it seems that IIS Caches them. So, after your done editing either of those files you need to open the web.config file and re-save it so it forces a refresh.
at moment i am using IIRF from codeplex.com ,its a isapi and it can distribute rules in 100 files and then you can include all those files into one file, so if i have to change one rule , i will goto specific file and fix it…all other files are remain untouched …. can i do same with URL rewrite as well ?
As what i have researched so far that we cant have multiple config sources defined in web.config , its very hard to maintain a rule.config file ,e.g i have 2000 rules which we keep on changing ,so if i dont split them in multiple files, it will be easy to break with one extra / in file , please help
I’m not familiar with IIRF. Config files are somewhat problematic, in that one error and your ASP.NET site will go down. I think you’re correct that you couldn’t split the rewrites into more than one file. AS far as adding/updated/deleting, if you use the II/S interface, it will take care of properly writing the config files, so they shouldn’t cause a crash. It doesn’t of course, validate that your rule will work. I’ve used ISAPI Rewrite from HeliconTech in the past. It’s very useful for mixed ‘nix Windows shops who already know a .htaccess file up down and a sideways.