Click here to Skip to main content
15,885,998 members
Articles / Web Development / IIS

URL Rewriting using ASP.NET for SEO

Rate me:
Please Sign up or sign in to vote.
4.73/5 (25 votes)
25 Feb 2009CPOL15 min read 164.7K   6.8K   130  
An article describing how to implement URL rewriting in ASP.NET using three different methods
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        body{ font-family: Tahoma; font-size: 12px;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>
        Url Rewriting Sample Website</h1>
        <h2>Written by Steve J. Riggall</h2><br />
        <br />
        This sample web site uses an HTTP handler to rewrite requests for *.html 
        resources.
        <br />
        <br />
        For this to work in IIS6 you need to add a resource mapping to the website 
        properties by clicking the "Configuration" button on the "Home Directory" tab 
        (or "Virtual Directory" tab for an application virtual folder), and adding a 
        mapping for the *.html to the ASP.NET ISAPI extension.&nbsp; This is usually 
        located at <strong>C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll</strong> but you 
        can always find and copy the properties for the *.aspx mapping if you are 
        unsure. You also need to ensure you un-check the option to make sure the 
        requested resource exists on the server.<br />
        <br />
        The rewriting should just work when hosted under IIS7 as the handler mappings 
        are already present in the web.config file.<br />
        <br />
        <br />
        <br />
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/FriendlyPage.ashx">Visit &quot;FriendlyPage.ashx&quot;</asp:HyperLink> (This is a rewritten page.  It should work with or without IIS mappings as *.ashx are already mapped to ASP.NET ASAPI)
        <br />
        <br />
        <asp:HyperLink ID="HyperLink2" runat="server" 
            NavigateUrl="~/UnfriendlyPage.aspx">Visit &quot;UnfriendlyPage.aspx&quot;</asp:HyperLink> (This is the aspx page that the above link is mapped to.)
        <br />
        <br />
        <asp:HyperLink ID="HyperLink3" runat="server" 
            NavigateUrl="~/ActualPage.html">Visit &quot;ActualPage.html&quot;</asp:HyperLink> (This is an actual static html page.  It should be served correctly.)
    
    </div>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Hidalgo Limited
United Kingdom United Kingdom
Steve is an MCP and senior software developer for Hidalgo Limited (http://www.hidalgo.co.uk).

Comments and Discussions