Search code examples
iisurl-rewritingiis-8url-rewrite-modulerequest-headers

Can you use request or response header in an IIS8 url rewrite map?


I have some rules set up to rewrite certain urls to make things a bit tidier, but am having issues with HTTPS as our server is only accessed via our load balancer using HTTP. The load balancer adds the request header Front-End-Https which I would like to use in place of the IIS server variable {HTTPS}. Is this possible?

Something like:

<rule name="RemoveAspxExtension" enabled="true" stopProcessing="true">
      <match url="(.*).aspx$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{URL}" pattern="\.axd$" negate="true" />
            <add input="{URL}" pattern="\.aspx/" negate="true" />
            <add input="{URL}" pattern="\.asmx" negate="true" />
            <add input="{URL}" pattern="\.ashx" negate="true" />
            <add input="{URL}" pattern="\.css$" negate="true" />
            <add input="{URL}" pattern="\.js$" negate="true" />
            <add input="{URL}" pattern="\.png$" negate="true" />
            <add input="{URL}" pattern="\.jpg$" negate="true" />
            <add input="{URL}" pattern="\.gif$" negate="true" />
            <add input="{URL}" pattern="\.xml$" negate="true" />
            <add input="{URL}" pattern="\.txt$" negate="true" />
            <add input="{URL}" pattern="\.html$" negate="true" />
            <add input="{URL}" pattern="/scripts/" negate="true" /> 
            <add input="{URL}" pattern="/styles/" negate="true" />
            <add input="{URL}" pattern="/secured/" negate="true" />
      </conditions>
      <action type="Redirect" url="{MapProtocol:{ToLower:{Front-End-Https}}}://{HTTP_HOST}/{R:1}"  appendQueryString="true" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="MapProtocol" defaultValue="http">
      <add key="on" value="https" />
      <add key="off" value="http" />
    </rewriteMap>
  </rewriteMaps>

Solution

  • Yes, you can use any HTTP Header by adding an {HTTP_ in front, som you can use {HTTP_Front-End-Https} and it should work.