This documentation is archived and is not being maintained.

HttpContext.RewritePath Method (String)

Rewrites the URL using the given path.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

public void RewritePath(
	string path


Type: System.String
The internal rewrite path.


The path parameter is null.


The path parameter is not in the current application's root directory.

The RewritePath(String) method redirects a request for a resource to a different path than the one that is indicated by the requested URL. If you have to reset the virtual path so that requests from the client for server resources resolve correctly, use the overload of this method that takes the rebaseClientPath parameter and set the parameter to false.

URL rewriting is useful when you want to restructure the pages in your Web application, and you want to make sure that people who have bookmarked old URLs can still use them after you have moved pages. URL rewriting enables you to transparently forward requests to the new page location.

If you want to enable a site to use URLs that are more user-friendly and are optimized for search engines, a more robust alternative is to use ASP.NET routing. For more information, see ASP.NET Routing.

The following example shows how to use the RewritePath method to enable a Web site to respond to URLs that do not reflect the file structure in the Web site. The first block of code is an ASP.NET Web page that is named RewritePath.aspx. It requires a query string. If the name of your site is WebSite1, the URL http://localhost/WebSite1/RewritePath.aspx?page=1 displays "Page 1" in the browser. The block of code that follows the Web page is the Application_BeginRequest event handler in the Global.asax file. This code intercepts requests for URLs such as http://localhost/WebSite1/page1 and converts them to the form that is required for RewritePath.aspx before they are processed. Therefore, the URL http://localhost/WebSite1/page1 invokes RewritePath.aspx with the query-string parameter that displays "Page 1" in the browser. If a URL such as http://localhost/WebSite1/page1 is received, an overload of RewritePath is invoked that enables you to provide a value for the PathInfo property as well as a query string parameter.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
        Label1.Text = "Page=" + Request.QueryString["page"] + " PathInfo=" + Request.PathInfo;

<html xmlns="">
<head id="Head1" runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

void Application_BeginRequest(Object sender, EventArgs e)
    string originalPath = HttpContext.Current.Request.Path.ToLower();
    if (originalPath.Contains("/page1"))
        Context.RewritePath(originalPath.Replace("/page1", "/RewritePath.aspx?page=page1"));
    if (originalPath.Contains("/page2"))
        Context.RewritePath(originalPath.Replace("/page2", "/RewritePath.aspx"), "pathinfo", "page=page2");

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.