Web.config rewrite that excludes certain URL's needed - windows

The standard web.config rewrite to turn all HTTP into HTTPS works fine
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
redirectType="Temporary" />
</rule>
</rules>
</rewrite>
URLS that begin with HTTP://192.168 should not be rewritten but I've been unable to change the code to do this. I've tried to change the match to HTTP://192.168 and then negate it but I think that affects the action line

Related

Enable htpps for a website hosted in windows server

Having the SSL certificate supposedly implemented on the server, what's the next step to have "https//" at the beginning of the URLs?
Thanks for the overwhelming help. I managed to resolve my problem and was actually very simple. I had to create a web.config file and copy paste the next code
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Force HTTPS" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.example.es/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
And after that, replace the url with the one from your own web. Simple but if you donĀ“t have any knoledge on the matter is actually very frustrating

When a dot exist in the middle of the URL, the rewriter works wrong

I have a project in asp net core 2.0.
I Have stablished the next Rule in the web.config:
<rewrite>
<rules>
<rule name="wwwroot" >
<match url="([\S]+[.](js|css|png|gif|jpg|jpeg))" />
<action type="Rewrite" url="wwwroot/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
When i acces to the url: www.foobar.com/lib/chart.js/dist/Chart.js for some reason the iis try to finde Chart.js in the path: wwwroot/lib/chart.js ignoring the last part of the url (/dist/Chart.js) when im expecting to take wwwroot/lib/chart.js/dist/Chart.js
Try to modify your regexp like this: ([\S]+[.](js|css|png|gif|jpg|jpeg))$ it should consider file extension if they at the end of your url
EDIT
To your rule you should exclude urls starting from wwwroot
<rule name="wwwroot" >
<match url="([\S]+[.](js|css|png|gif|jpg|jpeg))" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" negate="true" pattern="^/wwwroot" ignoreCase="true" />
</conditions>
<action type="Redirect" url="wwwroot/{R:1}" logRewrittenUrl="true" />
</rule>

How to Filter urls with URL rewrite rules

<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
I have used above url rewrite rule in my web.config file to enable SSL for the whole site.
Now I need to change the above rule to filter 2 urls which should work as http.
Let's say those urls as https://www.domain.com/owner/Marketing and https://www.domain.com/owner/getinfo.
Currently those urls are https hence above rules.So how can I change above rule to filter above 2 urls (i.e. http://www.domain.com/owner/Marketing and http://www.domain.com/owner/getinfo )?
I would add a separate rule to exclude urls that should not be redirected to https.
I cannot test it right now, as I don't have an IIS7 by the hand. but give it a try:
<rewrite>
<rules>
<rule name="Skip HTTPs" enabled="true" stopProcessing="true">
<match url="^(Marketing|getinfo)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="None" />
</rule>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
Note that you have to put that file into /owner/ directory as per your example.
you may have seen the documentation on that already - just pointing it out where i found it:
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
Also here is a maybe-duplicate of that question using lookahead regex rules: Rewriting URLs in IIS7

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>

Redirect URL in IIS 7.5

I want to all www domain to naked via IIS Rewrite engine. There are many domains pointing to same application.
Here is my rule :
^(www.)(.*)$
Action Type : Redirect
Redirect URL : {R:2}
Redirect Type : Permanent
When i test pattern for www.xxx.com
R:0 => www.stackoverflow.com R:1=> www. R:2 => www.stackoverflow.com
and that is fine.
What is wrong? And also should I include "http://" ?
Something like ^(www\.)(.+)$ should work when matching the http_host, but it might be better to specify the domain. From what I know of IIS (not much) and what it says on the net, something like:
<rewrite>
<rules>
<rule name="Redirect www.xxx.com to xxx.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com$" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
Oh, you said for any domain. If you want to make sure it ends in .com it should be something like
^(www\.)(.+)(\.com)$ against HTTP_HOST
.. oh, if you do that you need to back reference, so try something like this:
<rewrite>
<rules>
<rule name="Redirect www.domain.com to domain.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The c:1 is a back reference

Resources