How to rewrite URLs (url rewrite)

Address rewriting in ASP.NET is one of the most commonly used security features that has many benefits. Ease of indexing of the site by search engines, hiding the Query String and displaying the appearance of a dynamic site statically and increasing the relative security of the site are the most important of these benefits. In this article, we will explain to you about Url Rewrite.

The list of this article (by clicking on any title you will be transferred to that part) hidden
What is Url Rewrite?
Url Rewrite support in 2.0
The best URL rewriting tool

What is Url Rewrite?

The task of Url Rewriting is to convert a clear and meaningful URL into a real URL with a Query String to be interpreted by the engine. Consider the following two addresses:




To get information about what is query string , you are suggested to read our article.

In the example above, of course, the second address is more meaningful than the first address. At the same time, it somewhat reduces the possibility of hacking the site through the injection of Sql expressions. Let’s review the tools available for Url Rewriting.

Url Rewrite support in 2.0

After the emergence of 2.0, the news of the standard support of this technology for Url Rewriting was very interesting. For this purpose, in 2.0, you only need to make some changes in Web.Config:


<urlMappings enabled=”true”>
<add url=”~/show-5.htm”
mappedUrl=”~/show.aspx?id=5″ />


Please note that the above settings belong to the branch and must be defined in this section of Web.Config. In the example above, url is the address that the user sees and mapped Url is our real address. But as it can be seen from the appearances, this procedure will be useful only for times when the number of times of use is limited and if the number of addresses is high, managing them will be very difficult and time-consuming.

The best URL rewriting tool

To solve this problem, other tools have been made available to programmers. One of the best tools can be found at To use this tool, which is also Open Source, you can download its documentation and everything you need from the above address.
Although the documentation and materials presented on the above site are completely “comprehensive”, in the rest of this article, I will draw your attention to examples of UrlRewriting in

First, download the attached file of the article and transfer Intelligencia.UrlRewriter.dll to the Bin folder of your project. Now follow the steps below to read a fixed address:

Open the Global.asax file and code the Application_BeginRequest event as below. As the name of this event suggests, the code is executed when a request is sent to the engine.

[pre] if (Request.Url.ToString().Contains(“show.htm”))

In this example, the content of a page named show.htm, which is created virtually and does not exist externally, is read from another page named show.aspx.
Then open the web.config file and set its content as follows:


<section name=”rewriter” type=”Intelligencia.UrlRewriter.Configuration. RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter”/>

<add name=”UrlRewriter” type=”Intelligence.UrlRewriter.RewriterHttpModule, Intelligence.UrlRewriter”/>

<rewrite url=”show.htm” to=”show.aspx”/>


Please pay special attention to the location of the changes:

<configSections>: placed inside the <configuration> tag.
<httpModules>: placed
inside the <system.web> tag.
should be placed inside the <configuration> tag and outside and after the <system.web> tag.

Please note that you can also download the complete program code at the end of the program.

Hide Query String:
You may have thought how to rewrite virtual addresses with Query String. This work is also very simple and has minor differences from the above example.
To do this, open the Global.asax file again and code the Application_BeginRequest event as follows:

[pre] if (Request.Url.ToString().Contains(“show-(.+).htm”))

The sign (.+) represents a variable that takes the Query String value, and $1 in the next line is the same value (.+) and represents the Query String value in the real address. Also, change the rewrite tag as follows:

<rewrite url=”show-(.+).htm” to=”show.aspx?id=$1″/>

[/pre] If your address has more than one Query String parameter (for example three parameters), proceed as follows:

[pre] show-(.+)-(.+)-(.+).htm

Change the rewrite tag like the previous examples.
I think with the numerous examples in this article, you have found the skill to create addresses virtually.

tip :

Pay attention that in retrieving addresses with multiple Query String parameters in the real address field before &, amp; put the In addition, no special coding is required to read the Query String value, and considering the above example, after UrlRewriting, we can read the Query String value with the following command on the show.aspx page:

[pre] Response.Write((string)Request.QueryString[“id”]);