I'm trying to get the original URL from within my application (MVC 5) after a reverse proxy rewrite has occurred.
I've tried everything I can find e.g.
Any ideas?
Tried on IIS7 and IIS7.5 with ARR 3.0 and Url Rewrite 2.0
This answer is inspired by Setting HTTP request headers and IIS server variables in the IIS documentation. They do something similar, but oddly it avoids detecting whether the original URL was accessed with HTTP or HTTPS.
First, you need to have administrative access to your IIS server in order to set up a new allowed server variable in the URL Rewrite module. This is described in the linked article, but here are the basic steps:
HTTP
. For example, HTTP_X_MY_HEADER
is accessible as the X-MY-HEADER
header.Then, in your rewrite rule, set the server variable value to {CACHE_URL}
. You can do this through the UI, or directly in web.config, as shown below.
NOTE: be sure to set your match, conditions, and actions as needed.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="original URL sample" stopProcessing="true">
...
<serverVariables>
<set name="HTTP_X_MY_HEADER" value="{CACHE_URL}" />
</serverVariables>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The resulting header will explicitly include the port number, e.g. http://foo.example:80/bar
, so you may need to deal with that depending on your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With