i'm trying to force a url redirect (with the url rewrite module but for some reason it doesn't seem to be willing to match..)
my use case is as follows:
url: http://somesite.domain.com/?test=4
Now I'm trying to redirect this when it matches ?test=4 to http://somesite.domain.com/subsite/?test=5
I've tried this in multiple ways aleady by having a matching pattern of ^\?test=4 (on both the match url or the condition)
For some reason tho it isn't willing to trigger.
Is there something obviously wrong with a matching regex pattern of \?test=4 or should this work?
Full web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="^\?test=4" enabled="false" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\?test=4" />
<action type="Redirect" url="/subsite/?test=5" appendQueryString="true" />
<conditions>
</conditions>
</rule>
<rule name="2" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAny">
</conditions>
<action type="Redirect" url="/subsite/?test=5" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I'm fairly now to the url rewrite module so I'm going a bit by trial and error..
Any tips would be nice!
As far as I know, the url match will not match the query string. So your url rewrite rule will not work.
If you want to check the querystring, I suggest you could use IIS url rewrite querystring condition.
More details, you could refer to below url rewrite rule:
<rule name="MatchQueryString" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="test=4" />
</conditions>
<action type="Redirect" url="/subsite/?test=5" appendQueryString="false" />
</rule>
Result:
Related
I want to write redirect rules in IIS 10. I googled it and spent half of my day figuring out but no luck so I am posting it to get some solution.
https://testing.app.com/apptest should redirect to https://testing.app.com/apptest/account/login
https://testing.app.com/apptest/ should redirect to https://testing.app.com/apptest/account/login
https://test-apptest.testing.app.com/ should redirect to https://test-apptest.testing.app.com/account/login
https://test-apptest.testing.app.com should redirect to https://test-apptest.testing.app.com/account/login
But when user types url https://testing.app.com/apptest/account/login or https://test-apptest.testing.app.com/account/login then it should not redirect anywhere and it should stay as it is.
<rewrite>
<rules>
<rule name="Test1" stopProcessing="true">
<match url="account/login" />
<action type="None" />
</rule>
<rule name="Test2">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="testing.app.com" />
<add input="{REQUEST_URI}" pattern="^/apptest" />
</conditions>
<action type="Redirect" url="https://testing.app.com/apptest/account/login" appendQueryString="false" />
</rule>
</rules>
</rewrite>
We just add an anchor point to the regular expression so that precisely matches the segment ‘/apptest’.
Updated
<system.webServer>
<rewrite>
<rules>
<rule name="MyRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^((/apptest)?)/?$" />
</conditions>
<action type="Redirect" url="https://{http_host}{C:1}/account/login" />
</rule>
</rules>
</rewrite>
</system.webServer>
Explanation
Since the hostname changed and will subsequently be appended in the redirection URL, I replace it with {http_host} server variable to follow it. Besides, {Request_URI} will return the URL path and {C:1} will return either "/apptest" or "". therefore I append it into the redirection URL.
The meaning of every server variable.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule
Finally, please don’t forget to install the URL Rewrite extension before applying the rules.
https://www.iis.net/downloads/microsoft/url-rewrite
Here is a quick reference of the regular expression from Microsoft documentation.
https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
Feel free to let me know if the problem persists.
On my web.config file, first I need to redirect users requesting URL on http to https. Then, afterwards, I need to do further stuff, I need to make sure www is there in the beginning of the URL. When redirecting users from non-www to www, I want to use the https already set on the previous rule. But I want to do it dinamically, I don't want to hardcode 'https://' on the beginning of my rewrite rule.
The goal is: in the future, if for some reason I need to change this rule from https back to http (I don't think I will, but I may need this), I don't have to go on checking all the redirect rules below to change from 'https' to 'http'. If I use a variable (not sure I should call things inside brackets like {HTTPS} as 'variables'), all I have to do is change the first rule and ignore the rest.
Here is an example so things can get clearer; the key issue I am trying to solve is the item where I have written {WHAT TO USE HERE TO MAKE SURE I USE THE SAME PROTOCOL (HTTP/HTTPS) ALREADY DEFINED ABOVE?}:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<rewrite>
<rules>
<!-- The first rule will do a 301 redirect to https -->
<rule name="Redirect to https" stopProcessing="false">
<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>
<!-- Now, let's make sure we have www if request does not carry it -->
<rule name="Redirect to www" stopProcessing="false">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Redirect" url="{WHAT TO USE HERE TO MAKE SURE I USE THE SAME PROTOCOL (HTTP/HTTPS) ALREADY DEFINED ABOVE?}www.mydomain.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
</configuration>
Thank you so much, any help appreciated.
Best way is to use rewrite map to identify protocol. In your case it will be like that:
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="false">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.mydomain.com/{R:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
I am tring set the rule as below:
<rewrite>
<rules>
<rule name="Test" stopProcessing="false">
<match url="abcd.com/admin(.*)" />
<action type="Redirect" url="xyz.com/admin{R:1}" logRewrittenUrl="true" />
<conditions trackAllCaptures="true">
</conditions>
</rule>
</rules>
</rewrite>
When I try to access to url:abcd.com/admin/login but action not work. Please help me about that. Thanks!
The match url does not contain the domain, so you need to remove that:
<match url="^admin(.*)" />
How can I setup a rule to replace & with & in url?
This works: www.../home.asp?h=1&w=2
This fails: www.../home.asp?h=1&w=2
For starters, it should be noted that you can access the messed up w parameter as follows:
Request.QueryString("amp;w");
However, I expect you would like something a little more eloquent :)
Assuming that you have access to the IIS URL Rewrite module, (IIS 7 and above), you can add some rules to web.config as follows:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="One Bad Ampersand" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="^([^&]+)&([^&]+)$" />
</conditions>
<action type="Rewrite" url="{R:1}?{C:1}&{C:2}" appendQueryString="false" />
</rule>
<rule name="Two Bad Ampersand" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="^([^&]+)&([^&]+)&([^&]+)$" />
</conditions>
<action type="Rewrite" url="{R:1}?{C:1}&{C:2}&{C:3}" appendQueryString="false" />
</rule>
<rule name="Three Bad Ampersand" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="^([^&]+)&([^&]+)&([^&]+)&([^&]+)$" />
</conditions>
<action type="Rewrite" url="{R:1}?{C:1}&{C:2}&{C:3}&{C:4}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
What these rules do is check for an incoming & in the query string and replace it with &. I do not think it is possible to come up with a generic rule to handle an arbitrary number of occurrences. But, I have established a pattern for up to 3 occurrences that you should be able to follow to add as many as needed.
It should be noted that if you wish to redirect the user's browser, you may do so by changing the type attribute in each action from Rewrite to Redirect.
I am trying to add redirect rule 'www' when the request comes without the 'www' for the site. ie. http://example.com to http://www.example.com
Here is what the rule looks like:
<rewrite>
<rules>
<rule name="Add www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com" />
</rule>
</rules>
</rewrite>
Once i add this rule, the site goes to infinite loop and it just error out (page can't be displayed error message). The server is brand new and it might be missing some redirect components (if there is one). I did install URL Rewrite component and added rule in it.
Any suggestions?
Thanks.
You have not chosen a pattern syntax and so you using the default regular expression syntax. As a result your pattern matches both example.com and www.example.com and causes an infinite loop. Try this:
<rewrite>
<rules>
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>