Rewriting rule for & in url - iis

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="^([^&]+)&amp;([^&]+)$" />
</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="^([^&]+)&amp;([^&]+)&amp;([^&]+)$" />
</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="^([^&]+)&amp;([^&]+)&amp;([^&]+)&amp;([^&]+)$" />
</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.

Related

IIS redirect with url exception for stripe

I'm working on updating the stripe checkout from a website, I was doing it successfully on localhost but when I put it on live mode on a windows server it stopped working. The issue is that when I should be redirected to the checkout page from stripe, the url is altered and becomes something that doesn't make sense:
The correct url: www.checkout.stripe.com/pay/cs_...
The url that I get redirected to: www.mysite.com/pay/cs_..
I kept thinking what could be the causa of that and I think it's the URL rewrite rule that I have on the windows server. I would like to add an exception to the rule and allow the stripe checkout to initiate, but I have no idea how to do it.
Below is my web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:8008/{R:1}" />
</rule>
<rule name="HTTPS" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I figured it out. This is my final web.config
<configuration>
<system.webServer>
<urlCompression doDynamicCompression="false" />
<rewrite>
<rules>
<clear />
<rule name="ReverseProxyInboundRule">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="http://localhost:8008/{R:1}" />
</rule>
<rule name="stripe redirect in checkout" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="/pay/" />
</conditions>
<action type="Redirect" url="checkout.stripe.com{REQUEST_URI}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
</system.webServer>
My issue was not really understanding the meaning of the options in the URL Rewrite. I checked the course URL Rewrite for Developers and it was really helpful. I was able to solve the issue quickly.
The file you have shown contains the inbound rewrite rules only. But you have an issue with response from your server. Thus, you should fix the outbound rewrite rule in the right config file.

How to create a rewriteMap in IIS with dynamic path?

I'm trying to make a rewrite map in IIS to rewrite any call from domain.com/v/{PATH} to domain.com/api/v/{PATH} as i've changed my hosting logic and as an external service can't change the .GET request is making to domain.com/v/{PATH} in domain.com/api/v/{PATH} i have to rewrite any call in IIS..
I've created the following rewriteMap:
<rewrite>
<rewriteMaps>
<rewriteMap name="vmenuAuth">
<add key="/v/" value="/api/v/" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Regola di reindirizzamento1 per vmenuAuth">
<match url=".*" />
<conditions>
<add input="{vmenuAuth:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
But it works only if the url has ONLY the /v/ so if i write domain.com/v/ it rewrite the url to domain.com/api/v/ but if i try to add the path domain.com/v/VHGbrbfFHHTRfbFKSZ it will still remain the same without redirecting the user to domain.com/api/v/VHGbrbfFHHTRfbFKSZ
It seems that it is unnecessary to apply the URL Rewrite Maps under the circumstance.
We will redirect the URL to the new path as long as we match the “/v/” segment, is it right?
Please refer to the below configuration.
<rewrite>
<rules>
<rule name="abc" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{Request_URI}" pattern="/v(.*)" />
</conditions>
<action type="Redirect" url="/api/v{C:1}" appendQueryString="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MyMap" defaultValue="">
<add key="1234" value="HtmlPage1.html"></add>
</rewriteMap>
</rewriteMaps>
</rewrite>
Feel free to let me know if there is anything I can help with.

iis url rewrite - how to match pattern starting with "?"

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:

Azure web.config redirect from one page to another in wordpress

I want to redirect a page in Azure web.config with the url:
http://example.com/Vad+%E4r+V%E4rdskap__1053.html
to
http://example.com/what-is-welcoming/
I'd like it to work for http://www.example.com as well.
I tried adding my own rule in web.config with the following code:
<rule name="URL1" stopProcessing="true">
<match url="^Vad\+%E4r\+V%E4rdskap__1053\.html" ignoreCase="true" />
<action type="Redirect" url="/what-is-welcoming/" redirectType="Permanent" />
</rule>
I also tried without escaping the + signs.
There were some issues but I managed to solve the problem.
The first issue was a security setting for + signs in URL:s for ISS.
This was solved by using:
...
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
The code for the matching and redirect were as follows:
<rewrite>
<rules>
<rule name="vadarvardskap" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="example.com/Vad+%E4r+V%E4rdskap__1053.html" />
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="www.example.com/Vad+%E4r+V%E4rdskap__1053.html" />
</conditions>
<action type="Redirect" url="http://example.com/what-is-welcoming/" redirectType="Permanent"/>
</rule>
...

Force HTTPS via URL rewrite in web.config

I'm trying to force HTTPS via a web.config URL rewrite rule for a URL which includes a path.
eg http://my.domain.com/folder must be forced to use https://my.domain.com/folder
I'd also like to force HTTPS for all pages on their relative paths
eg http:// my.domain.com/folder/page.aspx forced to https:// my.domain.com/folder/page.aspx
This is what I have:
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
<add input=”{REQUEST_METHOD}” pattern=”^get$|^head$” />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
Try this URL rewrite rule:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<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>
</rules>
</rewrite>
</system.webServer>
</configuration>
Check your casing and the type of quotes you are using. Also, the 2nd input method is invalid - the pattern isn't valid a regex. This rule works for what you want:
<rule name="Redirect 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>

Resources