Host header (SERVER:) and URL Rewrite - url-rewriting

I want to hide the IIS version in my web server response. I am trying to do this via URLRewrite for my website.
My web.config looks like so:
<rewrite>
<outboundRules>
<rule name="RewriteServerSoftware" stopProcessing="true">
<match serverVariable="SERVER_SOFTWARE" pattern="." />
<action type="Rewrite" value="MyServer" replace="true" />
</rule>
</outboundRules>
</rewrite>
This however does not seem to remove the value of Server:Microsoft-IIS/8.0. What might I be missing?
My environment is Windows Server 2012 R2 and IIS 8.0, hosting an ASP.NET website

The correct server variable name is: RESPONSE_Server which follows the RESPONSE_headername format.
So your config should be:
<rewrite>
<outboundRules>
<rule name="RewriteServerSoftware" stopProcessing="true">
<match serverVariable="RESPONSE_Server" pattern=".+" />
<action type="Rewrite" value="MyServer" replace="true" />
</rule>
</outboundRules>
</rewrite>
One additional thing to note is that if you're using IIS 10.0 you also might need to add the following to your web.config:
<system.webServer>
<security>
<requestFiltering removeServerHeader="true" />
</security>
</system.webServer>
In my case with IIS 10.0 only using both URL-Rewrite and the removeServerHeader worked.
More Info: https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/

Related

Rewrite rule for windows server + IIS and Cakephp 3.x

I want to deploy my site to Windows hosting from linux. On Linux server everything working as expected. but few things are not working on windows server.
Please let me know what changes i need to do in web.config
My file system is:
-src
-webroot
--css
--about.html
--index.php
--contact.php
Now how i can access about.html directly http://www.example.com/about.html and similarly for other php files too.
these are rule i am using for now:
<rewrite>
<rules>
<rule name="Exclude direct access to webroot/*"
stopProcessing="true">
<match url="^webroot/(.*)$" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="Rewrite routed access to assets(img, css, files, js, favicon)"
stopProcessing="true">
<match url="^(assets|font|fonts|img|images|css|files|js|favicon.ico)(.*)$" />
<action type="Rewrite" url="webroot/{R:1}{R:2}"
appendQueryString="false" />
</rule>
<rule name="Rewrite requested file/folder to index.php"
stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="index.php"
appendQueryString="true" />
</rule>
</rules>
</rewrite>

Http Redirect within web.config using wildcards not working

My need is to tell IIS, within the web.config to ignore some URLs and to send a HttpRedirect to the client.
To do so, I added the following magic into web.config :
<system.webServer>
<httpRedirect enabled="true" httpResponseStatus="Temporary" exactDestination="true">
<add wildcard="*/iac*; " destination="http://someServer.smthg.be/iac$S$Q" />
</httpRedirect>
</system.webServer>
When I try to make a request, to eg ´myserver/iac/john?doe=hello´ I only receive a 404 not found instead of the 302 I expected.
<rewrite>
<rules>
<clear />
<rule name="test" stopProcessing="true">
<match url="^iac/(.*)" />
<action type="Redirect" url="http://someServer.test.be/iac/{R:1}" appendQueryString="true" redirectType="Temporary" />
</rule>
</rules>
</rewrite>

IIS Express 8.0 rewrite <rules> inside <outboundRules> not recognized

VS2010 IIS Express 8.0, trying to test rewrite rules in my development machine. I am adding this section to my web.config. Everything works until I add the 'rule' part inside the 'outboundRules' section, in which case I get the 500.52 error. I've tried all combinations or installing / uninstalling IIS, Windows Process Activation and all that, but nothing. Any ideas?
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<rewrite>
<rules>
<rule name="Rewrite to RewriteTest.aspx">
<match url="^RewriteTest/([0-9]+)/(g[_0-9a-z-]+)" />
<action type="Rewrite" url="RewriteTest.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
<outboundRules>
<rule name="Rewrite to clean URL" preCondition="IsHTML">
<match filterByTags="A" pattern="^/RewriteTest\.aspx\?id=([0-9]+)(?:&|&amp;)title=([_0-9a-z-]+)$" />
<action type="Rewrite" value="/RewriteTest/{R:1}/{R:2}" />
</rule>
<preConditions>
<preCondition name="IsHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>

mvc 4 url rewrite is giving me a 404.4 even though the rewrite looks ok

I'm trying to get the url rewrite to change a request such as
http://foo.bar.com/
to
http://bar.com/foo/
I've popped in my URL rewrite rule to the web config like this
<rewrite>
<rules>
<rule name="SiteReWrite" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://bar.com/{C:1}/"/>
<conditions>
<add input="{HTTP_HOST}" pattern="(.*)\.bar\.com" />
</conditions>
</rule>
</rules>
</rewrite>
but I get a 404.4 error. Looking at the Trace Log File I see this in there
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="WWW Server" Guid="{3A2A4E84-4C21-4981-AE10-3FDA0D9B0F83}"/>
<EventID>0</EventID>
<Version>1</Version>
<Level>4</Level>
<Opcode>42</Opcode>
<Keywords>0x0</Keywords>
<TimeCreated SystemTime="2012-12-03T05:54:01.237Z"/>
<Correlation ActivityID="{00000000-0000-0000-1000-0080030000FC}"/>
<Execution ProcessID="7312" ThreadID="3180"/>
<Computer>ULTRA</Computer>
</System>
<EventData>
<Data Name="ContextId">{00000000-0000-0000-1000-0080030000FC}</Data>
<Data Name="OldUrl">/</Data>
<Data Name="NewUrl">http://bar.com/foo/</Data>
</EventData>
<RenderingInfo Culture="en-US">
<Opcode>URL_CHANGED</Opcode>
</RenderingInfo>
<ExtendedTracingInfo xmlns="http://schemas.microsoft.com/win/2004/08/events/trace">
<EventGuid>{D42CF7EF-DE92-473E-8B6C-621EA663113A}</EventGuid>
</ExtendedTracingInfo>
</Event>
Which from the NewUrl looks like everything is peachy. Note, It looks like this instance was cached but I have seen the rule matches in the events previously.
If I enter the url http://bar.com/foo/ it works as it should.
I don't want the user to see the new url, they should think that they are on foo.bar.com not bar.com/foo. That's just for some routing I have in the mvc app.
Does anyone have any idea why it's doing this odd behavior? I'm using IIS express 8 for this, I haven't tried with IIS yet.
You should redirect and not rewrite:
<rewrite>
<rules>
<rule name="SiteReWrite" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://bar.com/{C:1}/"/>
<conditions>
<add input="{HTTP_HOST}" pattern="(.*)\.bar\.com" />
</conditions>
</rule>
</rules>
</rewrite>
If you really want to rewrite you have to set up IIS as a reverse proxy by installing the ARR (Application Request Routing) module.
Since I didn't want the user to see the url change I couldn't use the redirect. However what I should have mentioned is that all subdomains are going to the same site. Once I'd thought about it a bit more, and with a bit more reading on the rewrite module I modified an MSDN example like this; which works like a charm
<rewrite>
<rules>
<rule name="Rewrite subdomain">
<match url="(.*)" /> <!-- rule back-reference is captured here -->
<conditions>
<add input="{HTTP_HOST}" pattern="^([^.]+)\.bar\.com$" /> <!-- condition back-reference is captured here -->
</conditions>
<action type="Rewrite" url="{C:1}/{R:1}" /> <!-- rewrite action uses back-references to condition and to rule when rewriting the url -->
</rule>
</rules>
</rewrite>

ISAPI PHP REWRITE

A friend is having me figure out a way to modrewrite on his windows server. He is running IIS 6 and Isapi is about the only thing I can find. I am not familiar with it and have read some of the documentation, but can't quite wrap my head around it. He is wanting to rewrite these URLS to make them clean
www.domain.com/cat.php?CTGID=####
and
www.domain.com/pp.php?ID=##
How would I go about rewriting these two URLS to make them Clean in ISAPI. I have installed it on the Windows Server and do I put these rules in IISF.ini in his website folder or put these codes in the master IISF file? Any Help would be greatly appreciated!
Rewrite rules go in a web.config file or in the ApplicationHost.config.
As described in http://learn.iis.net/page.aspx/465/url-rewrite-module-configuration-reference/
Examples
<rewrite>
<rules>
<rule name="Force WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="Redirect from blog">
<match url="^blog/([_0-9a-z-]+)/([0-9]+)" />
<action type="Redirect" url="article/{R:2}/{R:1}" redirectType="Found" />
</rule>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
For IIS, if the ISAPI_Rewrite module is installed, $_SERVER["IIS_UrlRewriteModule"] is set and contains the version number of the module. So you can check for the existence of this server variable.

Resources