conditional redirect in web.config - redirect

I have a web.config redirect section that is intended to redirect HTTP requests to HTTPS. Here is the redirect section in my web.config file:
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
<outboundRules>
<rule name="Add the STS header in HTTPS responses">
<match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
<conditions>
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Rewrite" value="max-age=31536000" />
</rule>
</outboundRules>
</rewrite>
I am not sure what flexibility can be implemented but I want this section to work as-is EXCEPT for a specific URL. Specifically, I want all HTTP traffic to continue to be redirect from HTTP to HTTPS EXCEPT when the request is HTTP://{HOST}/symphony/default.aspx.
Is this possible?

You can try adding a condition to the rule to match the request HTTP://{HOST}/symphony/default.aspx, and then use the negate attribute to negate the result of matching.
More information about negate you can refer to this link: Rule pattern properties.

Related

Redirect url with a hashcode to another with same hashcode in IIS10

I need to redirect a url with a variable hashcode to another url with that same hashcode in IIS10 (Windows Server 2019).
Example:
https://www.example.com/hello/sd54effg1g5s11d5111dwds21fds2f1ffd
Needs to redirect to:
https://subdomain.example.com/hello/sd54effg1g5s11d5111dwds21fds2f1ffd
At the moment i have this as a rule in the web.config:
<rule name="rulename" stopProcessing="true">
<match url="^hello/[a-zA-Z0-9]+$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(https:\/\/www\.)example.com$" />
</conditions>
<action type="Redirect" url="https://subdomain.{HTTP_HOST}/{R:1}" />
</rule>
Firstly, the {HTTP_HOST} is will not match the https part in the url. So you should use ^www.example.com$ instead of ^(https:\/\/www\.)example.com$.
Besides, you should use https://subdomain.example.com/{R:1} instead of the https://subdomain.{HTTP_HOST}/{R:1} to achieve your requirement.
Details ,you could refer to below url rewrite rule:
<rule name="rulename" stopProcessing="true">
<match url="^hello/[a-zA-Z0-9]+$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://subdomain.example.com/{R:0}" />
</rule>

IIS URL Rewrite + Redirect + does not match the querystring

I need to redirect to another website when the url contains www. and the query-string does not match the specfic value. I am always getting redirected irrespective of the condition
<rewrite>
<rules>
<rule name="Redirect to Landing Page" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_Host}" pattern="www.dev.MyWebpage.com/MCPrivacyNotice" negate="true" />
</conditions>
<action type="Redirect" url="https://www.myWebPage.com" />
</rule>
</rules>
</rewrite>
As you described, so you need to redirect when
Domain is www.dev.MyWebpage.com
Request uri is /MCPrivacyNotice
So I think this may be your answer
<rule name="Redirect to Landing Page" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="www.dev.MyWebpage.com" />
<add input="{REQUEST_URI}" pattern="/MCPrivacyNotice" negate="true" />
</conditions>
<action type="Redirect" url="myWebPage.com" />
</rule>
Note that logicalGrouping="MatchAll" to match all condition. In your question and your update you used logicalGrouping="MatchAny" that means every request from domain www.dev.MyWebpage.com will be redirected
One more thing, /MCPrivacyNotice is {REQUEST_URI} or PATH_INFO not QUERY_STRING you should choose the right module. Check this for detail https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
Hope this helps

How to redirect all traffic to https & non-www in web.config?

I'm using an SSL certificate and IIS. In my web.config I want to redirect all website traffic in the following order:
all http --> https
all www --> non-www
You want to use the redirect module in order to do this. This intercepts the request as it's coming in to the server and changes it according to your instructions.
Here is an action to redirect all to https:
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
and here is one to redirect www to non-www:
<conditions> <add input=”{HTTP_HOST}” pattern=”^example\.com$” negate=”true” /> </conditions> <action type=”Redirect” url=”http://example.com/{R:1}” />
There are plenty of articles around the web and StackOverflow when you include both "web.config" and "redirect" in your search.
Make sure that when you set up the rules you only use stopProcessing="true" on the final rule. If that appears on the first rule then the second rule will never execute.
i find the answer
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="NonWwwRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.yoursit\.com$" />
</conditions>
<action type="Redirect" url="http://yoursite.com/{R:1}" />
</rule>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<system.webServer />

URL ReWrite with un-named parameter

I would like this URL
http://alpha.Mysite.io/Car/Details/{4f2a95ed-3582-4486-8ef6-8a8e6731161f}
to redirect to
http://alpha.Mysite.io/Car/index.htm?Action=Details&guid={4f2a95ed-3582-4486-8ef6-8a8e6731161f}
The GUID parameter ({4f2a95ed-3582-4486-8ef6-8a8e6731161f}) is differenet each time so needs to be passed to the redirected URL.
I have the basic redirect working using a ReWriteMap, however as soon as I add the nameless GUID paramter I get a 404 instead
Here's my section from webconfig
<rewrite>
<rules>
<rule name="Rewrite rule1 for carAPI">
<match url=".*" />
<conditions>
<add input="{carAPI:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="carAPI">
<add key="/car/details/" value="/car/index.asp?Action=Details" />
</rewriteMap>
</rewriteMaps>
</rewrite>
In your case, you do not need rewrite map. You can achieve everything with single rule:
<rule name="Rewrite rule1 for carAPI" enabled="true" stopProcessing="true">
<match url="^Car/Details/([0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})" />
<action type="Rewrite" url="/Car/index.htm?Action=Details&guid={R:1}" appendQueryString="false" />
</rule>
This rule will match urls like Car/Details/{any valid .net guid} and rewrite them into Car/index.htm?Action=Details&guid={guid}

web.config redirect by ip

For website security, I would like to redirect a single IP address, or a few IP addresses, to a different domain using web.config. I'm new to this. I know how to limit access or block certain IPs, but is there an easy way to redirect? Thanks!
To redirect certain IP address you will need to use the URL redirect rules engine available for IIS 7
Check this link for instructions on how to redirect by IP address:
https://webmasters.stackexchange.com/questions/31509/web-config-to-redirect-except-some-given-ips
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="(.*)$" ignoreCase="false" />
<conditions>
<add input="{REMOTE_HOST}" pattern="^123\.123\.123\.123" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="/coming-soon.html" />
</rule>
</rules>
</rewrite>
Couldn't reply to Victor's comment, so I'll put my edited code here rather than suggest changes.
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="(.*)$" ignoreCase="false" />
<conditions>
<add input="{REMOTE_HOST}" pattern="^123\.123\.123\.123" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="http://www.domain-to-redirect-to/coming-soon.html" />
</rule>
</rules>
</rewrite>
The change here is that it's redirecting to a new domain as the asker wanted, rather than to a page on the same domain.
Note that if you want to redirect to coming-soon.html on the same domain, you will need to change your match rule, otherwise you will be put into a redirect loop.
Thus:
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="(.*)coming-soon.html$" ignoreCase="false" negate="true" />
<conditions>
<add input="{REMOTE_HOST}" pattern="^123\.123\.123\.123" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="/coming-soon.html" />
</rule>
</rules>
</rewrite>