What Is URL Rewrite Module & How It Can Be Used?

What Is URL Rewrite Module & How It Can Be Used?

The URL Rewrite Module is an extension software for IIS (Internet Information Services). URLs should be created so that they are easy to remember for the users and easy to find for the search engines. The URL Rewrite Module enables web administrators to develop and implement rules that assist them in this task. URLs generated by web applications may not necessarily be user-friendly. The URL Rewrite Module will replace these with the friendlier version of the URL. It can be used to implement complex rewrite logic.
 
what is URL Rewrite ModuleRule templates, rewrite maps, etc. are used and integrated into the IIS manager. These are then used by the administrators to set up the rules that define the defining behaviour of the URL. Besides, these rules can be based on the HTTP Headers, HTTP Response, server variables or even on programming logic.

In addition, the rewrite providers wrote in .NET, regular expression mapping, wildcard mapping allow the web administrator to build powerful rules. The rules are defined such that the URLs generated have a canonical and consistent format of the host name. The URL Rewrite Module also provides support for a multiple of features. All these assist the administrator in creating the rewrite rules.
 
The URL that are generated can easily be replaced with the search engine friendly version generated by the URL Rewrite Module. The links have the possibility to be edited in the HTML format that the web application generates. Additionally, web administrators can also perform redirects and send custom responses. It has a rule based response writing engine. There are some outbound rules that define how the logic compares part of the response. These rules also specify what is to be done in case the comparison is successful. If you don't want these rules to be applied to every response then, you can use pre-conditions to check if the rules need to be applied. 
 
what is URL Rewrite ModuleFor better management, the URL Rewrite module has been securely made together with the IIS Manager. It supports both the caching in user-mode or kernel-mode, so that the performance is further improved. It supports Failed Requests Tracing as well that results in better troubleshooting.
 
The URL Rewrite Module has custom rewrite providers that can be used in case the rewrite logic cannot be expressed as regular expressions. This also comes in handy when rewriting decisions are to be made based on data that is stored outside the web.config file, i.e. on a database or text files. There is no need to go through the entire HTML file to search for a match. You can exclusively specify exactly which HTML tags needed to be scanned through. 
 
Other than the URLs, even the HTTP Response headers and IIS Server Variables are able  to be set using the rewrite rules. The outbound rules that are able to be used to revise the HTTP Response headers and even set up new ones.
 
When there are too many rewrite rules, there is a possibility that some server variables that should not be modified might accidently be changed. To solve this problem, you need to add the server variables that can be modified.

How URL Rewrite Module Can Be Used?

Now, let us take a look at how the URL Rewrite Module can be used.
 
How URL Rewrite WorksBefore you can open and use URL Rewrite Module you need to have IIS 7 or above and URL Rewrite Module 2.0 installed on your computer. To see how it works, we take a simple ASP.NET page that inputs the web variables in the server and outputs the values in the web browser.
 
First, create a file called 'article.aspx' in the %SystemDrive%\inetpub\wwwroot\ folder and write the following code in it.
How URL Rewrite Module Works - code 1
Save this file and browse to http://localhost/article.aspx to check ifthe file was rendered properly in the browser.
 
The next step is to create a rewrite rule. We will attempt to change the URL from http://localhost/article/342/some-article-title to http://localhost/article.aspx?id=342&title=some-article-title.
 
For this, you need to go to the IIS manager and select Default Website. Then, in the feature view, click on URL Rewrite as shown below.
How URL Rewrite Works 1
After that in the Actions pane located on the right-hand side click on Add Rules.
URL Rewrite Module 2
You will now see an Add Rules dialog box. Select Blank Rule and click on the Ok button.
How URL Rewrite Works 3
The next step is defining the actual rule. Specify rule name, the pattern that has to be used for matching the URL String, the selectable set of conditions and action to perform whether the pattern matches. The name must exclusively identify the rule. Here we will use Rewrite to article.aspx. In the pattern bow, write the string ^article/([0-9]+)/([_0-9a-z-]+). Select the action as Rewrite.
How URL Rewrite Works 4
Scroll down and you will see the Rewrite URL Text Box. Enter the string article.aspx?id={R:1}&title={R:2} there.
How URL Rewrite Works 5
Click on Apply to save the rule.
 
The rewrite rule will be saved in the web.config file or ApplicationHost.config file. If you open the file then, you will see the following code in it.
How URL Rewrite Module Works - code 2
Finally, to test this rewrite, open your browser and go to http://localhost/article/234/some-title. You will see the following page.
How URL Rewrite Works 6
Related Articles