21 March 2018

Redirect Virtual Directory to External Website using URL Rewrite

I recently had a client with an externally facing IIS server using the Default Website, and multiple Virtual Directories.  They wanted everyone accessing one particular Virtual Directory to be redirected to a completely separate website.  The ultimate goal in mind was to ensure that only internal users connecting to http://hostname/virtualdirectory will have access to the page.

After look into the best way to make this work, I found that utilising URL Rewrite on their IIS server would give the best solution.

After replicating the setup in a test lab, I managed to get the URL Rewrite working, to only redirect to an external website when someone accessed http://subdomain.domain.com/virtualdirectory

The following config was entered into the web.config file for the specific Virtual Directory.

<system.webServer>
  <rewrite>
    <rules>
      <rule name="External Redirect" stopProcessing="true">
        <match url="Test4" negate="true" />
        <conditions>
          <add input="{HTTP_HOST}" ignoreCase="true" negate="true" pattern="IIS"/>
        </conditions>
        <action type="Redirect" url="http://www.google.com.au" redirectType="Found" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

The items in bold require changing to match your specific IIS setup.  These are currently set to replicate my test lab environment.













When navigating to http://iis/test4, it loads the page.  When navigating to http://subdomain.domain.com/test4, it redirects to Google.  The setting is specific to the Virtual Directory, and other VDs are not affected by this.

1 comment: