URL Rewrite in IIS7

iis-7rewrite

My company's website has a directory http://website.net/files that has obscure data half of the company uses. It doesn't fit with our new site structure and I'd like to move it to a new server with the dns http://files.website.net. The problem I'm having is that many of the users will still have links similar to http://website.net/file/app.exe

I've been told that URL Rewrite is the best option to force a redirect while maintaining the query but I seem to be missing something. Any help would be appreciated.

enter image description here

web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="wordpress" patternSyntax="Wildcard">
          <match url="*" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php" />
        </rule>
                <rule name="files" stopProcessing="true">
                    <match url="^files/(.*)" ignoreCase="true" />
                    <action type="Redirect" url="http://downloads.openeye.net/files/{R:1}" logRewrittenUrl="true" />
                </rule>
      </rules>
    </rewrite>
    <httpErrors>
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" prefixLanguageFilePath="" path="/index.php?error=404" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>
</configuration>

Best Answer

Multiple issues in your screenshot:

  1. The regex pattern you have in the Pattern field is only going to match numbers after the last slash in the URL. "files/subdir/1234" will match. "files/subdir/app.exe" will not match. You need ^files/([_0-9a-z-]+)/(.*) in the pattern field. Or if all you need is anything with the /files, you can use ^files/(.*). The Test Pattern feature is your friend here.

  2. I would check Ignore Case, but that's up to you for your particular scenario.

  3. Action type should be "Redirect"

  4. Assuming you're using ^files/(.*) for your regex pattern, the Redirect URL should be: http://downloads.openeye.net/files/{R:1}

This would mean that someone who enters http://yourdomain.com/files/whatever.exe would get redirected to http://downloads.openeye.net/files/whatever.exe

Or if they have a longer URL like http://yourdomain.com/files/dir1/dir2/whatever.exe, it will still get tacked on the end of the new URL (http://downloads.openeye.net/files/dir1/dir2/whatever.exe).