Trying to figure out how to do wildcards for querystrings. Right now this works, but it has to be an exact match.
I have this rewrite rule.
<rule name="Redirect rule1 for Redirects ReSv">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" />
</rule>
My rewrite maps are like:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/contact-us.aspx" value="/contact/" />
<add key="/contact-us/request-information.aspx" value="/contact/" />
</rewriteMap>
</rewriteMaps>
Trying to get something like this:
/contact-us.aspx?q=t&1=2
To redirect to:
/contact/
And include the querystring? so..
/contact/?q=t&1=2
Please us {URL} variable instead of {REQUEST_URI}. Then Rewritemap will be able to wildcard the query string.
<rule name="Redirect rule1 for Redirects ReSv" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{Redirects:{URL}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
Related
I'm trying to figure out a url rewrite rule to rewrite http requests into https only when a specific path is accessed like below. I've tried quite a few things that in the test pattern seem as though they should work but it never does.
url accessed: http://example.com/books/test.css
I need to check for the http:// and /books/ to form the proper url below.
url needed: https://example.com/books/test.css
A request of http://example.com/book/test.css should be ignored.
You can specify the patten in the <match> element :
<rule name="Force HTTPS for /books/" enabled="true">
<match url="^/books/.*$" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" redirectType="Permanent"
url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" />
</rule>
or by adding a new <condition>
<rule name="Force HTTPS for /books/" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{REQUEST_URI}" pattern="^/books/.*$" />
</conditions>
<action type="Redirect" redirectType="Permanent"
url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" />
</rule>
I need to redirect non-www URLs to www URL for both HTTP and HTTPS URLs. I tried following rules in web.config.
<rule name="Redirect to 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 to WWW https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
It works perfectly for non-ssl URL but in case of SSL it redirect from https://example.com to http://www.example.com.
For a safer rule that works for both Match Any and Match All situations, you can use the Rewrite Map solution. It’s a perfectly good solution with the only drawback being the ever so slight extra effort to set it up since you need to create a rewrite map before you create the rule. In other words, if you choose to use this as your sole method of handling the protocol, you’ll be safe.
You can create a Rewrite Map called MapProtocol, you can use {MapProtocol:{HTTPS}} for the protocol within any rule action.
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect"
url="{MapProtocol:{HTTPS}}://www.example.com/{R:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
Reference
Other answers here are unnecessary long, here's the rule i use (just copy and paste) :
<rule name="ensurewww" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
This will redirect to the www version while preserving the HTTP and HTTPS protocol
Hope this helps.
Since 2018, consider to use everytime https (SSL) !
So I use redirect to www and to https together.
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
more info here :
https://forums.realmacsoftware.com/t/effective-july-2018-google-s-chrome-browser-will-mark-non-https-sites-as-not-secure/18597
I know this is an old post but it's not fully answered. I ended up extending Satpals answer
First rule catches http + www and second one catches non-www
For some reason i could not use MapProtocol in the fist Rule but it works with https written directly into the url.
<rewrite>
<rules>
<rule name="Special case www & HTTPS off" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="SeeOther" />
</rule>
<rule name="Redirect to www & HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.example.com/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
This worked fine for me:-
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^myExample.in$" />
</conditions>
<action type="Redirect" url="https://www.myExample.in/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
This post is basically for those who need to redirect non-www to www URL in windows hosting.
In web.config file as below code:
<system.webServer>
<rewrite>
<rules>
<rule name=”Redirect example.com to www.example.com HTTP” patternSyntax=”ECMAScript” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^example.com$” />
<add input=”{HTTPS}” pattern=”off” />
</conditions>
<action type=”Redirect” url=”http://www.example.com/{R:0}” redirectType=”Permanent” appendQueryString=”true”/>
</rule>
<rule name=”Redirect example.com to www.example.com HTTPS” patternSyntax=”ECMAScript” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^example.com$” />
<add input=”{HTTPS}” pattern=”on” />
</conditions>
<action type=”Redirect” url=”https://www.example.com/{R:0}” redirectType=”Permanent” appendQueryString=”true”/>
</rule>
</rules>
</rewrite>
</system.webServer>
In the above code please note there are two rules used one for HTTP and another for HTTPS. To avoid clashing of HTTP and HTTPS in the same rule, here used two separate patterns.
I hope it works for you guys…!!
Follow the Step Below
Login to your website's cPanel
Click on Hosting Setting
Select your preferred domain as www.example.com
I would like to match exactly https://internet.dev.local/KYR url (without / into end) and redirect or rewrite to https://internet.dev.local/KYR/ (with /).
I am trying the following rule but it matches other URLs as well e.g. https://internet.dev.local/KYR/Admin/Form/Default.aspx?signup=false, which is wrong.
so how can I achieve this?
<rule name="Static redirect" patternSyntax="ExactMatch" stopProcessing="true">
<match url="https://internet.dev.local/KYR" negate="true" />
<conditions>
</conditions>
<action type="Redirect" url="/Login/?ReturnUrl=/Member/KYR/" redirectType="Permanent" />
</rule>
If you need to test the host and protocol you have to put it in the conditions, not in the global rule.
Following your example, it would be as follow:
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url="KYR" />
<action type="Redirect" url="/KYR/" redirectType="Permanent" />
<conditions>
<add input="{HTTP_HOST}" pattern="internet.dev.local" />
<add input="{HTTPS}" pattern="on" />
</conditions>
</rule>
I have changed the url in the action because your question says:
redirect or rewrite to https://internet.dev.local/KYR/ (with /).
EDIT:
To get it to work on any host (with or without SSL), just remove the conditions:
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url="KYR" />
<action type="Redirect" url="/KYR/" redirectType="Permanent" />
</rule>
I'm using the IIS Rewrite Module on IIS7.5. My mappings is in a text file in the structure:
[old url], [new url]
So something like this works:
products/abc, http://test.com/new/products/abc
This uses the following rule in my web.config
<rule name="FileMapProviderRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{FileMapProvider:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" redirectType="Permanent" />
</rule>
What will my rule be if i want querystrings to be passed too? So i want this to work:
products?sku=123, http://test.com/new/products/123
products?sku=789, http://test.com/new/products/789
I solved this by the following rule:
<rule name="Products" patternSyntax="Wildcard" stopProcessing="true">
<match url="products" />
<conditions>
<add input="{QUERY_STRING}" pattern="sku=*" />
</conditions>
<action type="Redirect" url="http://test.com/new/products/{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
I Have some problems with redirecting to another URL based on the query string parameters. I want to redirect users which enter www.domain.com/signup.aspx?p=1 to:
www.domain.com/signup
<rule name="Signup Redirect 1" stopProcessing="true">
<match url="signup\.aspx\?p=1" />
<conditions logicalGrouping="MatchAll" />
<action type="Redirect" url="signup" redirectType="Temporary" />
</rule>
Now when they enter www.domain.com/signup.aspx?p=2 they must go to:
www.domain.com/signup/promocode
<rule name="Signup Redirect 2" stopProcessing="true">
<match url="signup\.aspx\?p=2" />
<conditions logicalGrouping="MatchAll" />
<action type="Redirect" url="signup/promocode" redirectType="Temporary" />
</rule>
The above rules don't work. What is the right way to do this? Thanks in Advance.
Gr
Martijn
A more robust method of using a value to select a destination is to use Rewrite Maps. The map is essentially a lookup table. This doesn't require a new rule (and an additional evaluation of the URL against a pattern on every request) for every new path.
<rules>
<rule name="Signup Redirect Map" stopProcessing="true">
<match url="^signup\.aspx$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="p=([^&]+)" />
<add input="{Signups:{C:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:2}" redirectType="Temporary" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="Signups">
<add key="1" value="signup" />
<add key="2" value="signup/promocode" />
<add key="3" value="signup/newcode" />
<add key="n" value="signup/futureproof" />
</rewriteMap>
</rewriteMaps>
Definitions:
{C:1} is a backreference to the first condition match: the query string value.
{Signups:{C:1}} is an instruction to look up {C:1} in the Signups map.
{C:2} is a backreference to the second condition match: the value from the Signups map.
See if this works a bit better:
<rule name="Signup Redirect 1" stopProcessing="true">
<match url="signup\.aspx$" />
<conditions>
<add input="{QUERY_STRING}" pattern="p=1" />
</conditions>
<action type="Redirect" url="signup" redirectType="Temporary" />
</rule>
<rule name="Signup Redirect 2" stopProcessing="true">
<match url="signup\.aspx$" />
<conditions>
<add input="{QUERY_STRING}" pattern="p=2" />
</conditions>
<action type="Redirect" url="signup/promocode" redirectType="Temporary" />
</rule>