Razor Migration Notes 2: Use URL Rewrite to maintain your Page rankings (SEO)
This is the second note of the series:
1: Moving a SitemapPath Control to ASP.NET Web Pages
My current Web Site was built using ASP.NET 2.0 and WebForms, that means that all of my pages have the extension .aspx. While moving each page to use ASP.NET Web Pages their extension is being changed to .cshtml, and while I’m sure I could configure it in a way to get them to keep their aspx extensions it is a good opportunity to “start clean”. Furthermore, in ASP.NET WebPages you can also access them without the extension at all, so if you have /my-page.cshtml, you can also get to it using just /my-page. Given I will go through this migration I decided to use the clean URL format (no extension) and in the process get better URLs for SEO purposes, for example, today one of the URLs look like https://www.carlosag.net/Articles/configureComPlus.aspx but this would be a good time to enforce lower-case semantics and also get rid of those ugly camel casing and get a much more standard a friendly format for Search Engines using “-“, like: https://www.carlosag.net/articles/configure-com-plus.aspx.
Use URL Rewrite to make sure to keep your Page Ranking and no broken links
The risk of course is that if you just change the URLs of your site you will end up not only with lots of 404’s (Not Found), but your page ranking will be reset and you will loose all the “juice” that external links and history have provided to it. The right way to do this is to make sure that you perform a permanent redirect (301) from the old URL to the new URL, this way Search Engines (and browsers) will know that the content has permanently moved to a new location so they should “pass all the page ranking” to the new page.
There are many ways to achieve this, but I happen to like URL Rewrite a lot, so I decided to use it. To do that I basically created one rule that uses a Rewrite Map (think of it as a Dictionary) to match the URL and if it matches it will perform a permanent redirect to the new one. So for example, if /aboutme.aspx is requested, then it will 301 to /about-me:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect for OldUrls" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{OldUrls:{REQUEST_URI}}" pattern="(.+)"/>
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="OldUrls">
<add key="/aboutme.aspx" value="/about-me"/>
<add key="/soon.aspx?id=1" value="/coming-soon"/>
<add key="/Articles/configureComPlus.aspx" value="/articles/configure-com-plus"/>
<add key="/Articles/createChartHandler.aspx" value="/articles/create-aspnet-chart-handler"/>
<add key="/Articles/createVsTemplate.aspx" value="/articles/create-vs-template"/>
...
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration>
Note that I could have also created a simple rule that would change the extension to cshtml, however I decided that I also wanted to change the page names. The best thing is that you can do it incrementally and only rewrite them once your new page is ready or even switch back to the old one later if any problems occur.
Summary
Using URL Rewrite you can easily keep your SEO and pages without broken links. You can also achieve lots more, check out: SEO made easy with IIS URL Rewrite 2.0 SEO templates – CarlosAg