Why is this IIS7 rewrite performing a redirect

iis-7redirect

I have a rewrite rule responding with a 301 redirect but I don't know why.

I'm on WinHost shared hosting, using IIS 7 rewrite to host another domain I own from a subfolder of my account.

By default, WinHost points your other domains at the root of your account. So my goal is two-fold:

  1. I want to keep this other domains files physically separate in a subfolder
  2. I want to hide said subfolder from the address bar

So that:

http://myotherdomain.com

Gets served from:

E:\account_root\myotherdomain

So using the IIS 7 rewrite module, I generated this rule.

<xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Test" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="(^|\.)myotherdomain\.com$" />
                    </conditions>
                    <action type="Rewrite" url="myotherdomain/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

I want requests for myotherdomain.com to offset into the myotherdomain subfolder.

This is working, but only if no paths are in the request. If I request http://myotherdomain.com I receive a 200 response & in my browser I see the default document located at E:\account_root\myotherdomain. No redirection occurs.

If you add a path to the request, e.g. http://myotherdomain.com/test, now I receive a 301 redirect to the rewritten URL:

Response: HTTP/1.1 301 Moved Permanently
Location: http://myotherdomain.com/myotherdomain/test/

Which the browser then GETs:

Request: GET /myotherdomain/test/ HTTP/1.1

The rewrite rule then runs on IIS again, and ultimately IIS attempts to serve the default document located at:

E:\account_root\myotherdomain\myotherdomain\test

Which doesn't exist:

Response: HTTP/1.1 404 Not Found

So the rewrite itself appears to be working; what I don't understand is why IIS is throwing a 301 redirect into the mix, but only when there is a path present in the request.

Best Answer

Isn't it redirecting to add the trailing / ?