IIS URL Rewrite (1/4)
The URL Rewrite module serves 2 main purposes:
- Rewrite the URL passed in by the client, which is useful for instance to create user friendly URLs that then point to the actual URLs where the resources are located.
- Create HTTP redirections when a page has moved (either temporarily or permanently) or doesn't exist anymore.
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.
|Figure 1: URL Rewrite installation|
|Figure 2: URL Rewrite installation|
|Figure 3: 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.
|Figure 5: IIS Manager with URL Rewrite|
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.
|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.
|Figure 7: New URL Rewrite rule|
We select the blank rule template. The screen on Figure 8 is shown.
|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
to the new
|Figure 9: 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.
|Figure 11: New URL Rewrite rule|
Our new rule is now visible in the list of rules as shown in Figure 12.
|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
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:
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.
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
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.
|Figure 13: 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
The URL displayed in the browser though still shows
http://localhost/alias.html so the rewrite is transparent to the user.
|Figure 15: URL Rewrite rule application|
blog comments powered by Disqus