URL Rewrite

URL REWRITE: " Different ways";

 

URL REWRITE USING GLOBAL ASAX:  

This very simple way and very useful. In this way we can rewrite N number of pages and there is no need extra server configuration. Only you have to use Application_BeginRequest event. Inside this event use Context.RewritePath method to set which URL will execute internally in code behind. Here is the code:

 

First, we créate a WebForm file and add href link we want to request.

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="dansau.Default" %>

<!DOCTYPE html>

<html xmlns="https://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<a href="/myrequestedpage/dansau/www.contoso.com/">Submit</a>

</div>

</form>

</body>

</html>

 

 

If we request that href link we will not able to get resource so we will get 404 status code error.

What we have to do is creating a Global.asax file and place the following code into Application_BeginRequest.

RewritePath handles internal redirects. It is common to have page locations that are easy to remember and convenient. But we want to redirect them to a central place in the code. We rewrite paths in ASP.NET using the RewritePath method.

BeginRequest. Application_BeginRequest receives each request that arrives at the ASP.NET application. It will receive requests for images, aspx files, and other files as people try to access those files on your server

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.SessionState;

namespace dansau

{

public class Global : System.Web.HttpApplication

{

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Session_Start(object sender, EventArgs e)

{

}

protected void Application_BeginRequest(object sender, EventArgs e)

{

HttpContext context = HttpContext.Current;

string path = context.Request.Path.ToLower();

int lastContent = path.LastIndexOf("/myrequestedpage/");

if (lastContent == -1)

{

return;

}

else

{

context.RewritePath("~/requested.aspx?file=" + 2, false);

}

}

protected void Application_AuthenticateRequest(object sender, EventArgs e)

{

}

protected void Application_Error(object sender, EventArgs e)

{

}

protected void Session_End(object sender, EventArgs e)

{

}

protected void Application_End(object sender, EventArgs e)

{

}

}

}

 

 

When we request page and then click in Submit href link

We will be able to request /requested.aspx file from that request.

Summary. We rewrote paths in ASP.NET using the RewritePath method. Friendly URLs are valued more in search engine placement and are more likely to be bookmarked and shared than "ugly" or hard-to-read URLs.

 

 


 

 

 

 

URL REWRITE USING CONFIG FILE:

URL rewriting in web.config is only use for small number of pages. Here you have to rewrite every page manually in web.config. I am writing sample code for 2 urls.
configuration>

<system.web>

 

<urlMappings enabled="true">

<add url="~/Content/Page22.aspx"

mappedUrl="~/Default.aspx"/>

<add url="~/Content22/Page9.aspx"

mappedUrl="~/requested.aspx"/>

</urlMappings>

 

If we point to that request will be able to be mapped to the Default.aspx file.

 

 


 

 

URL REWRITE MODULE:

 

6

 

Easily replace Web application URLs to produce user and search engine friendly results

URL Rewrite permits Web administrators to easily replace the URLs generated by a Web application in the response HTML with a more user friendly and search engine friendly equivalent. Links can be modified in the HTML markup generated by a Web application behind a reverse proxy. URL Rewrite makes things easier for outbound response content and headers rewriting with outbound rewrite rules that work with HTTP request and response headers and with IIS server variables.

The Microsoft URL Rewrite Module 2.0 for IIS 7 and above enables IIS administrators to create powerful customized rules to map request URLs to friendly URLs that are easier for users to remember and easier for search engines to find. You can use the URL Rewrite module to perform URL manipulation tasks, some of which include:

  • Define powerful rules to transform complex URLs into simple and consistent Web addresses.
  • Easily replace Web application URLs to produce user and search engine friendly results.
  • Rewrite URLs based on HTTP headers and IIS server variables.
  • Perform redirects, send custom responses, or stop HTTP requests based on the logic expressed in the rewrite rules.
  • Control access to Web site content based on URL segments or request metadata.

The URL Rewrite module is available both as a download that you can install on your own IIS server, and on Microsoft's Azure Web Sites cloud platform.

 

 

Create a Web page

In order To show you how the URL rewrite module works, you will use a simple ASP.NET page. This page reads the Web server variables and displays their values in the browser. It also constructs a hyperlink by using the server variables and then puts that link into the response HTML.

The test page

  1. Create a file named home.aspx in the following folder: %SystemDrive%\inetpub\wwwroot\

Copy the following ASP.NET markup, paste it into the file and save the file:
<%@ Page Language="C#" %>

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

<html xmlns="https://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>URLREWRITE</title>

</head>

<body>       Original URL:

<%= Request.ServerVariables["UNENCODED_URL"] %>

 

Final URL

<%= Request.ServerVariables["SCRIPT_NAME"] + "?" + Request.ServerVariables["QUERY_STRING"] %>

 

 

<a href="<%= Request.ServerVariables["SCRIPT_NAME"] + "?" +    Request.ServerVariables["QUERY_STRING"] %>">Here</a>.

</body>

</html>

 

 

Open a Web browser and request the following URL in order to make sure that the page renders correctly: https://localhost/home.aspx

Add an Inbound Rewrite Rule

The next step is to add a rule that rewrites URLs that have the following format:

https://localhost/home/333/sometitle

These URLs will be rewritten to have a format like the following:

https://localhost/home.aspx?id=333&title=sometitle

To add the inbound rewrite rule:

  1. Open the web.config file located in the following location: %SystemDrive%\inetpub\wwwroot\
  2. Under the /configuration/system.webServer element, add the following and then save the file: <rewrite> <rules>    <rule name="Rewrite url to home.aspx">      <match url="^home/([0-9]+)/([_0-9a-z-]+)" />      <action type="Rewrite" url="home.aspx?id={R:1}&amp;title={R:2}" />    </rule> </rules> </rewrite>

Testing the Inbound Rewrite Rule

You can now test that the inbound rewrite rule is working as designed.

To test the inbound rewrite rule:

Open a Web browser and request the following URL:

https://localhost/home/333/sometitle

You can see that because of the inbound rule it is possible to access this web page by using a simple and user friendly URL structure.


ASP.NET ROUTING:

this is a way to rewrite url if well this does the same as url rewrite as for user friendly and search engine url the main purpose is to dispache  urls provided this match with the url patten [route] configured. On the other hand, rewrite module analyze and modify url in accordance with  configuration done. Please see the chapter ASP.NET Routing for more information.