2

I'd like to set a request header (HTTP_HOST to be precise) from Web.config, using the IIS URL Rewrite module, on Azure Websites. Basically I'd like to have something like this in my site's Web.config:

<system.webServer>
  <rules>
    <clear />
    <rule name="My rule" enabled="true">
      <match url=".*" />
      <serverVariables>
        <set name="HTTP_HOST" value="my value" />
      </serverVariables>
      <action type="None" />
    </rule>

This results in an error that HTTP_HOST is not allowed to be set. This is normal and with standard IIS the next step would be to add HTTP_HOST to the <allowedServerVariables> element to applicationhost.config directly or through AppCmd. However I couldn't find any hints on being able to access this config somehow.

Is it possible to somehow modify the apphost config, or add allowed server variables somehow else?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Piedone
  • 2,693
  • 2
  • 24
  • 43

2 Answers2

7

It is possible to alter Azure's ApplicationHost.config by applying xdt transformations.

Upload the file to the /site and restart your site for the changes to to take effect:

ApplicationHost.xdt

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
      <rewrite>
        <allowedServerVariables>
          <add name="HTTP_HOST" xdt:Transform="Insert" />
        </allowedServerVariables>
      </rewrite>
    </system.webServer>
</configuration>

See also:

  1. https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples
  2. http://azure.microsoft.com/nl-nl/documentation/articles/web-sites-transform-extend/
Joris Talma
  • 71
  • 2
  • 6
  • Thank you. I think this came with Site Extensions. – Piedone Oct 17 '14 at 20:54
  • 1
    Might want to be careful using `xdt:Transform="Insert"` because it may corrupt your web app instance altogether as it will try to add a duplicate entry in your `applicationgHost.config` everytime it is evaluated. Probably safer to use `xdt:Transform="InsertIfMissing"` instead – Frank Fu Nov 14 '18 at 23:30
  • You also need to specify `xdt:Locator="Match(name)"` otherwise XDT only matches the `` element name, which isn't useful at all! – Dai Aug 02 '20 at 06:54
1

Expanding on Joris' answer, you should use xdt:Transform="InsertIfMissing" and xdt:Locator="Match(name)" otherwise it won't work the way you expect it to (here's an example of it not working as-expected, and another example).

So your applicationHost.xdt should look like this:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
      <rewrite>
        <allowedServerVariables>
          <add name="HTTP_HOST" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)" />
        </allowedServerVariables>
      </rewrite>
    </system.webServer>
</configuration>
Dai
  • 141,631
  • 28
  • 261
  • 374