LearnFast.Ninja

URL rewrite create URL with htm

This is a simple code to do URL rewrite in 3 steps. The logic is very basic it is only replace the aspx extension to htm.

Step 1: Create new static URL Rewrite class.

public static class URL_Rewrite

{

    public static string Create_URL_With_Htm(string String_URL)

    {

        string ans = String_URL.ToLower().Replace(".aspx"".htm");

        return ans;

    }

}


Step 2: In the Global.asax add this code in the Application_BeginRequest event.

protected void Application_BeginRequest(object sender, EventArgs e)

{

    // URL Rewrite - START --------------------------------------

    string originalPath = HttpContext.Current.Request.Path.ToLower();

    if (originalPath.Contains(".htm"))

    {

        HttpContext.Current.RewritePath(originalPath.Replace(".htm"".aspx"));

    }

    // URL Rewrite - END ----------------------------------------

}


Step 3: In the Web.Config file add this handler.

<configuration>

  <system.webServer>

    <handlers>

      <add name="htm-ASP.NET" path="*.htm" verb="*" type="ASP.global_asax" resourceType="Unspecified" preCondition="integratedMode" />

    </handlers>

  </system.webServer>

</configuration>


Result:


Tags

asp.net

url-rewrite url-rewriting global-asax global rewritepath htm begin-request beginrequest aspx