question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IISUrlRewrite: {HTTP_HOST} contains port number

See original GitHub issue

As 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''.

image

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:open
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
korggycommented, Jan 28, 2020

It’s by design that ~HOST_NAME~ HTTP_HOST contains the port since that variable is intended to contain the value of the Host header.

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 the HTTP_HOST variable and the <port> portion corresponding to SERVER_PORT.

0reactions
analogrelaycommented, Jan 28, 2020

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IISUrlRewrite: {HTTP_HOST} contains port number #9713
I have a project that runs on, say, ports 5000 (http) and 5001 (https). ... IISUrlRewrite: {HTTP_HOST} contains port number #9713.
Read more >
In IIS URL Rewrite 2.0, why does HTTP_HOST include the ...
From this we can see that the port number is included in the HTTP_HOST variable, contrary to the documentation referenced above. This adds...
Read more >
IIS URL Rewrite HTTP to HTTPS with Port
I'm playing around with IIS 7 rewrite module 2.0 but I'm banging my head against the wall. Any insight is appreciated! BTW the...
Read more >
In IIS URL Rewrite 2.0, why does HTTP_HOST include ...
From this we can see that the port number is included in the HTTP_HOST variable, contrary to the documentation referenced above. This adds...
Read more >
How to hide/remove the port number in the url
I have a url: http://localhost:8080, now I want to remove the port number 8080, how can I do it? Internet Information Services.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found