Dot Net Perls

urlMappings for Redirects - ASP.NET

by Sam Allen

Problem

Redirect requests in ASP.NET from one location to a new location without using much code. You want to use urlMappings in ASP.NET's config files. You want to redirect Googlebot and users alike to the new locations.

Solution: ASP.NET urlMappings element

We can use urlMappings. This is a special tag you can put in your Web.sitemap. Here is a table of some "old style" URLs that I want to map to new URLs. You can see the new URLs had SEO improvements with more keywords.

urlmappedUrl
~/Content/Page10.aspx~/Content/Int-Parse-Conversion.aspx
~/Content/Page13.aspx~/Content/Post-Pre-Build-Macros.aspx
~/Content/Page25.aspx~/Content/Programming-Blogs.aspx

Question: how can I add urlMappings to my website?

By adding markup to your Web.config file. Remember, the ~ symbol, called a tilde, is a reference to the root of the virtual application directory. That's the root directory of your website. The following code example shows the urlMappings tag.

<!-- Example usage of urlMappings. You will need to customize the 'add'
     tags within the urlMappings element. -->

<urlMappings enabled="true">

    <!-- Redirect the pages in Google's index... -->
    <add url="~/Content/Page22.aspx"
        mappedUrl="~/Content/ASP-TreeView-Recursion.aspx"/>
    <add url="~/Content/Page9.aspx"
        mappedUrl="~/Content/Directed-Acyclic-Word-Graph.aspx"/>

</urlMappings>
  1. Open Web.config.
    Web.config is part of your website project in the Solution Explorer. You will need to double-click on it.
  2. Find the system.web element.
    Locate the <system.web> element and its closing tag. Put your <urlMappings> block into a section in the middle of that section. That's it!

Summary: using urlMappings in ASP.NET

Use urlMappings as a quick fix for redirecting requests on your ASP.NET server. There are other methods that may be more powerful. Use the RewritePath method in ASP.NET and Global.asax for a more flexible approach. [ASP.NET - Rewrite URLs With RewritePath - dotnetperls.com]

Finally, you can instruct the server to send all 404 errors to a certain page. That page can then handle the errors or redirect on its own. [ASP.NET - Server.Transfer to 404 Page - dotnetperls.com]

Dot Net Perls
About
Sitemap
ASP.NET
Cache Examples and Overview
Custom ASHX Handler Tutorial
Global Variables Example
HtmlTextWriter Use
Page_Load Event, AJAX Tutorial
New
Occurrence Count of String
StartsWith String Examples
© 2008 Sam Allen. All rights reserved.