IISUrlRewrite: {HTTP_HOST} contains port number
See original GitHub issueAs someone noted long before me the {HTTP_HOST} variable doesn’t only contain the host but also the port when using IISUrlRewriteOptions. I have a project that runs on, say, ports 5000 (http) and 5001 (https). I want to redirect all clients, except specific one, to HTTPS, which results in a rewriterule like:
<?xml version="1.0" encoding="utf-8" ?>
<rewrite>
<rules>
<rule name="Force HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="Cisco" negate="true" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
The result is that any request (http://example.net:5000/foo/bar is rewritten to https://example.net:5000/foo/bar). Note that it is rewritten correctly from http to https but that the port is included where the https port should be 5001 and not 5000.
Unless we’re using default ports (80, 443) I don’t see how to create this rule correctly other than hardcoding the url. The only answer on SO suggesting to use {SERVER_NAME} results in a System.FormatException: 'Unrecognized parameter type: 'SERVER_NAME', terminated at string index: '20''.

This goes for the IIS URL Rewrite rules (as above) but I’ve tested this with Apache rewrite rules as well and it has the exact same problem.
I’m using .Net core 2.2 but judging from the stackoverflow link this issue goes way back (2011 even) and I’m pretty sure something, somewhere, will break if this behavior is changed. So I suggest leaving {HTTP_HOST} as-is and maybe consider introducing a {HTTP_HOSTNAME} and {HTTP_HOSTPORT} or something.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (5 by maintainers)

Top Related StackOverflow Question
If this is by-design then the documentation at URL Rewrite Module Configuration Reference should be updated. Currently that page indicates that the URL will be decomposed as
http(s)://<host>:<port>/<path>?<querystring>with the<host>portion specifically corresponding to theHTTP_HOSTvariable and the<port>portion corresponding toSERVER_PORT.@korggy those are docs for the IIS Rewrite module, not ASP.NET Core, so our team doesn’t manage them. I suggest filing a bug on https://github.com/MicrosoftDocs/iis-docs for that team to look at.