Server variable used in URL Rewrite rule? - iis

In IIS, is it possible to get a value for a server variable to use in a URL Rewrite rule condition?
In my applicationHost.config file I have this:
<location path="api.domain-staging.site">
<system.webServer>
<aspNetCore>
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
And I want to add a condition into URL rewrite something like {HOST_ASPNETCORE_ENVIRONMENT} = "Staging" to be able to determine if the rule is used or not.
Is this possible?

Environment variables are not "server variables"
URL Rewrite rules do not support environment variables, so you might consider ASP.NET Core URL Rewrite middleware

Related

Use HTACCESS to Remove SERVER_SOFTWARE from ServerVariables

I am trying to remove SERVER_SOFTWARE from ServerVariables for security / PCI Compliance. We are running IIS 8.5 on Win Server 2012 R2 Standard.
I saw this, but it is modifying web.config. Host header (SERVER:) and URL Rewrite
I tried using "Header unset SOFTWARE" but i dont think it's being called correctly and I cannot figure out the correct setup. We are using Helicon ISAPI_Rewrite version 3.1.
Can this be done via HTACCESS?
I also tried doing the URL_REWRITE per here: https://port135.com/change-remove-response-headers/ I added the RESPONSE_Server variable, but it's still showing SERVER_SOFTWARE = Microsoft-IIS/8.5
Thanks.
Looks like I fixed this. For anyone that finds this, I just removed everything and put it back in place manually in web.config as the FIRST item inside the system.webServer - it did NOT work using URL Rewrite. Note - i was trying to replace with "0", but that could have been part of the issue as well, not sure. This EXACT web.config text worked:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<outboundRules rewriteBeforeCache="true">
<rule name="Remove Server header">
<match serverVariable="RESPONSE_Server" pattern=".+" />
<action type="Rewrite" value="" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>

URL Rewrite Redirection

I have configured a website in IIS with a virtual directory. (applicationHost.config file is given below )
<application path="/" applicationPool="TestMovieSearch">
<virtualDirectory path="/moviesearch" physicalPath="C:\inetpub\wwwroot\fullmovielist" />
</application>
I am using URLRewrite to redirect user to a specific html page. I have the below webconfig file placed in this path.
Path : C:\inetpub\wwwroot\fullmovielist
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Catch all for movie search redirection">
<match url="/" negate="true" />
<action type="Rewrite" url="/MovieSearch/action/movies.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
When I tried the URL http:\localhost\MovieSearch, its properly getting redirected to movies.html page. I just need to know how the web.config file matches with virtual directory and
properly redirect to movies.html page.
Note: I am a newbie to web.config file. So any help would be appreciated.
According to my understanding, you don’t need a rewrite rule, but you want to know how this rewrite rule works, is that right?
I guess there is also an action folder in your fullmovielist folder, which contains movies.html. (If not, it is the application of the MVC structure you are using.)
If you want to know how the url matches the rule and rewrites to new one, it is better to use fail request tracing.
http://localhost/moviesearch,the part of "/moviesearch" match the "/" and rewrite to http://localhost/moviesearch/action/movies.html.
You can know more rules and ways about url rewrite from document.

How to force IIS to use a specific value for the http host header during a rewrite

I'm trying to create a rewrite rule that configures a web site to be used as a reverse proxy, but I'm having some issues setting the host http header to a predefined value different from the domain used in the rewrite (the objective is to use IIS as a reverse proxy for forwarding urls to sendgrid with a custom host value).
In order to illustrate the problem, I've created 2 web sites named url and sendgrid with the bindings url.com and sendgrid.net (I've added custom entries for these names on the hosts file so that the names can be resolved). Now, I need to redirect all requests received on the url web site to the sendgrid website, making sure that the request to the sendgrid web site sets the host http header to url.com. I've started by adding a rewrite rule to the url web site that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="http://sendgrid.net/{R:1}" logRewrittenUrl="true" />
<serverVariables>
<set name="HTTP_HOST" value="url.madeira.gov.pt" />
</serverVariables>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The applicationhost.config file has also been updated so that the HTTP_HOST can be changed:
<location path="url">
<system.webServer>
<rewrite>
<allowedServerVariables>
<add name="HTTP_HOST" />
</allowedServerVariables>
</rewrite>
</system.webServer>
</location>
In order to see what's going on, I've activated Failed Request Tracing and I've noticed that the host header file defined through the previous rule is not applied. I can see that the rule is processed and that the HTTP_HOST header is processed (SET_SERVER_VARIABLE), but when the request is rewritten, it will always set the http host to sendgrid.net (instead of setting it to url.com):
So, is there a way to force the use of a specific value to the host header when a IIS web site is configured to be used as a reverse proxy?
try to set the preserveHostHeader to true by following the below steps:
1)open IIS manager, select the server node.
2)double clic configuration manager.
3)from the section drop down select system.webServer/proxy
4)set preserveHostHeader to true
Note: if you are trying to change the request header it is not possible by using iis URL rewrite rule.

Is it possible to configure IIS 7.5 output caching for an individual page?

We'd like to use IIS 7.5 output caching for an individual .aspx file, but after some reading at iis.net, it seems to me the caching can only be configured for various file extensions, but not for individual files. Is that correct or did I miss something?
Thank you.
Just change view to "content" select file, and config it. In extension add eq .js or .php. This config will be only to this file.
You can check this in:
eq
web.config
<location path="cachedfile.php">
<system.webServer>
<caching>
<profiles>
<add extension=".php" policy="CacheForTimePeriod" kernelCachePolicy="CacheUntilChange" duration="00:30:00" />
</profiles>
</caching>
</system.webServer>
</location>

Rewrite rules for response headers in IIS 7 (replacing the cookie path)

I have to port my web application from apache to IIS 7 and got into trouble with the proper configuration.
In the apache configuration, I configured some mod rewrite stuff (in order to communicate with an apache active mq) like this:
#Reverse-Proxy to ActiveMQ AJAX-Interface
ProxyPass /foo/bar/amq http://localhost:8161/foo/amq/
ProxyPassReverse /foo/bar/amq http://localhost:8161/foo/amq/
ProxyPassReverseCookiePath /foo /
I've tried to configure the IIS 7 by using ApplicationRequestRouting.
The rewrite rule in the request for replacing the /foo/bar to the localhost adress does already work, but I've some problems to define a rule for setting up the correct cookie path in the response.
I've already found an article about manipulating responses here.
For me, it looks like with II7 I can only manipulate the HTTP body of the response.
How can I manipulate the response header in a way to edit the cookie path?
The cookie path in the response header looks like this:
Set-Cookie: JSESSIONID=1lu7hn253csbh11jax27k2i072;Path=/foo
The Path should be edited to "Path=/".
Thank for your time and your help
Rolf
This should do it
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<remove name="Update Cookie Path" />
<rule name="Update Cookie Path">
<match serverVariable="RESPONSE_Set_Cookie" pattern="^(.*; path=/)foo$" />
<conditions />
<action type="Rewrite" value="{R:1}" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
Check the more detailed reference.

Resources