IIS URL Rewrite (3/4)

Using wildcards

The rules we have written so far were only affecting a very limited set of URLs. Wildcards and regular expressions are useful when an entire group of URLs need to be rewritten. We'll first look at wildcards and later on we'll look at regular expressions.

Our first rule with wildcards

Let's assume that we need to redirect all URLs in the olddir directory to the mystuff directory. Figures 1 and 2 show how to write such a rule.

Redirection for an entire directory
Figure 1: Redirection for an entire directory


Redirection for an entire directory
Figure 2: Redirection for an entire directory

We use the * wildcard in the pattern to match any subdirectory of olddir. The behaviour we want is that if a user requests the http://localhost/olddir/index.html URL the response should be a redirection to http://localhost/mystuff/index.html. However it must also work if he requests http://localhost/olddir/otherpage.html, he should then get a redirect to http://localhost/mystuff/otherpage.html. Therefore the Redirect URL depends on the requested URL. The IIS Rewrite module provides variables that can be used in the Redirect URL and that contain the parts of the request matched by the pattern. For instance {R:0} contains the entire part of the request that matched the pattern, {R:1} contains the part of the request that was matched by the first wildcard. So in our case we use the expression mystuff/{R:1}.

With this redirection in place we use cUrl to fetch some URLs and show the redirection works as expected. First we ask for http://127.0.0.1/olddir/index.html and confirm that we are redirected to http://127.0.0.1/mystuff/index.html

curl result for http://127.0.0.1/olddir/index.html
C:\Users\Xavier Leclercq>curl --verbose http://127.0.0.1/olddir/index.html
* About to connect() to 127.0.0.1 port 80 (#0)
*   Trying 127.0.0.1...
* Adding handle: conn: 0x248cf2b790
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x248cf2b790) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /olddir/index.html 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/index.html
* Server Microsoft-IIS/8.5 is not blacklisted
< Server: Microsoft-IIS/8.5
< Date: Thu, 05 Jun 2014 19:18:03 GMT
< Content-Length: 158
<
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://127.0.0.1/
mystuff/index.html">here</a></body>* Connection #0 to host 127.0.0.1 left intact

Then we fetch http://127.0.0.1/olddir/subdir/index.html and confirm that we are redirected to http://127.0.0.1/mystuff/subdir/index.html

curl result for http://127.0.0.1/olddir/subdir/index.html
C:\Users\Xavier Leclercq>curl --verbose http://127.0.0.1/olddir/subdir/index.html
* About to connect() to 127.0.0.1 port 80 (#0)
*   Trying 127.0.0.1...
* Adding handle: conn: 0x97913bb7d0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x97913bb7d0) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /olddir/subdir/index.html 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/subdir/index.html
* Server Microsoft-IIS/8.5 is not blacklisted
< Server: Microsoft-IIS/8.5
< Date: Thu, 05 Jun 2014 19:18:47 GMT
< Content-Length: 165
<
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://127.0.0.1/
mystuff/subdir/index.html">here</a></body>* Connection #0 to host 127.0.0.1 left
 intact

A rule with two wildcards

To show how the {R:n} variables each represent a part of the string matched by a wildcard we now show a rule with two wildcards.

We update the rule above with the following pattern olddir/*/*/index.html. This will match any index.html file that is in a subdirectory of a subdirectory of the olddir directory. When a string matches this rule {R:1} will contain the part of the string matched by the first * and {R:2} will contain the part of the string matched by the second *. With that in mind we update the Redirect URL with the following value: mystuff/{R:2}/{R:1}/index.html. Note how we inverted the order of the subdirectories to better show what happens.

Now if for instance we request the URL http://127.0.0.1/olddir/s1/s2/index.html we will be redirected to http://127.0.0.1/mystuff/s2/s1/index.html as shown by the cUrl request below.

curl result for http://127.0.0.1/olddir/s1/s2/index.html
C:\Users\Xavier Leclercq>curl --verbose http://127.0.0.1/olddir/s1/s2/index.html

* About to connect() to 127.0.0.1 port 80 (#0)
*   Trying 127.0.0.1...
* Adding handle: conn: 0x523654b720
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x523654b720) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /olddir/s1/s2/index.html 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/s2/s1/index.html
* Server Microsoft-IIS/8.5 is not blacklisted
< Server: Microsoft-IIS/8.5
< Date: Thu, 05 Jun 2014 19:20:14 GMT
< Content-Length: 153
<
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://127.0.0.1/
mystuff/s2/s1/index.html">here</a></body>* Connection #0 to host 127.0.0.1 left intact

blog comments powered by Disqus

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

Home
Contact Us
Search