IIS URL Rewrite (1/4)

Purpose

The URL Rewrite module serves 2 main purposes:

Installing the URL Rewrite module

The URL Rewrite module can be installed using the Web Platform Installer. Launch the Web Platform Installer and search for the URL Rewrite module as shown in figures 1 to 4.

URL Rewrite installation
Figure 1: URL Rewrite installation


URL Rewrite installation
Figure 2: URL Rewrite installation


URL Rewrite installation
Figure 3: URL Rewrite installation


URL Rewrite installation
Figure 4: URL Rewrite installation

After the installation the new URL Rewrite icon is visible in the IIS Manager as shown on Figure 5.

IIS Manager with URL Rewrite
Figure 5: IIS Manager with URL Rewrite

HTTP Redirection

The HTTP protocol allows the server to return to the client a status message indicating that the requested resource has moved (either permanently or temporarily) and containing the new URL that can be used to retrieve the resource. The typical use case is web page that has moved to a new location.

In this section we'll see how we can set up such a redirection using the URL Rewrite module. This example assumed we did set up the virtual directory mystuff as shown in the virtual directories tutorial . If we click on the URL Rewrite icon the screen on Figure 6 will be shown. There are currently no URL Rewrite rules.

URL Rewrite rules
Figure 6: URL Rewrite rules

Let's add a new rule by selecting the "Add Rule(s)..." menu on the right. The popup shown in Figure 7 is displayed.

New URL Rewrite rule
Figure 7: New URL Rewrite rule

We select the blank rule template. The screen on Figure 8 is shown.

New URL Rewrite rule
Figure 8: New URL Rewrite rule

We fill in the form as shown on figures 9 and 10. This creates a rule that will redirect the myoldstuff URL to the new mystuff URL.

New URL Rewrite rule
Figure 9: New URL Rewrite rule
New URL Rewrite rule
Figure 10: New URL Rewrite rule

Once we have filled in the form we click on the "Apply" menu on the right. The screen confirms that the new rule has been created as shown in Figure 11.

New URL Rewrite rule
Figure 11: New URL Rewrite rule

Our new rule is now visible in the list of rules as shown in Figure 12.

New URL Rewrite rule
Figure 12: New URL Rewrite rule

If we now enter the http://127.0.0.1/myoldstuff URL in a browser we will be redirected to http://127.0.0.1/mystuff.

This is because behind the screens the browser received a HTTP 301 status code back from the server in response to our request for http://127.0.0.1/myoldstuff. That response also contained the URL where the resource can now be found. The browser then made a request using that URL: http://127.0.0.1/mystuff.

To confirm that this is indeed what happened we use cUrl to make the same request. The output is shown below. You can see the HTTP 301 status code and the Location header that indicates the new URL where the resource has been moved to. The body of the request gives that information in human readable form in case the browser doesn't automatically handle the redirection.

curl result
C:\Users\Xavier Leclercq>curl --verbose http://127.0.0.1/myoldstuff
* About to connect() to 127.0.0.1 port 80 (#0)
*   Trying 127.0.0.1...
* Adding handle: conn: 0xeacb10b770
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0xeacb10b770) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /myoldstuff HTTP/1.1
> User-Agent: curl/7.33.0
> Host: 127.0.0.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Content-Type: text/html; charset=UTF-8
< Location: http://127.0.0.1/mystuff
* Server Microsoft-IIS/8.5 is not blacklisted
< Server: Microsoft-IIS/8.5
< Date: Wed, 28 May 2014 23:11:30 GMT
< Content-Length: 147
<
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://127.0.0.1/
mystuff">here</a></body>* Connection #0 to host 127.0.0.1 left intact

URL Rewrite

This section presents another possible scenario when developing a website. We may want to provide a URL that is an alias for another URL. For instance because the actual URL is very long or difficult to remember. The difference with the redirection case we presented in the previous section is that this will be completely transparent for the user. In the redirection case the browser is responsible for doing the actual redirection and the user will see the new URL displayed. In the URL rewrite case we will show now, the server does the redirection in a way that is completely transparent for the user.

As before we select the blank rule template and fill in the form as shown on figures 13 and 14.

New URL Rewrite rule
Figure 13: New URL Rewrite rule


New URL Rewrite rule
Figure 14: New URL Rewrite rule


If we now point our browser to http://localhost/alias.html it fetches the page at http://localhost/mystuff/index.html. The URL displayed in the browser though still shows http://localhost/alias.html so the rewrite is transparent to the user.

URL Rewrite rule application
Figure 15: URL Rewrite rule application

blog comments powered by Disqus

Copyright(c) 2006-2017 Xavier Leclercq | Privacy policy

Home
Contact Us
Search