Simple Redirect using Rewrite Module - iis

I want add a rewrite rule to IIS 7 rewrite.
I want to redirect a user from page
http://localhost/myapp/register.html to http://localhost/myapp/register/register.html
And similar for other pages.
Any help?
Can I do the same using a rewrite action?
Using IIS7 and Windows Server 2008.
Thanks.

I'm not sure if it suits your needs but you can try this one (and maybe make some small adjustments if the case):
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="Register Redirect">
<add key="register.html" value="/register/register.html" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Redirect rule1 for Register Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^(.*)/register/(.*)(\.html)?$" negate="true" />
</conditions>
<action type="Redirect" url="{HOST}/register/{R:1}" appendQueryString="true" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>

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.

web.config - use current page protocol (http/https) as a variable in a redirect rule

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>

IIS rewrite change filename

I have some old URLs in a forum application points to http://mydomain.tld/showthread.php?p=xxxx where xxxx is the an integer id.
My new forum application uses viewtopic.php?p=xxxx and I want to handle this using IIS web.config rewrite rules.
I have tried to add the last rule as the following:
<rewrite>
<rules>
<rule name="Redirect www to non www" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://domain.tld/{R:1}" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.tld" negate="true" />
</conditions>
</rule>
<!-- this for change from showthread.php to viewtopic.php -->
<rule name="Rewrite" stopProcessing="true">
<match url="(.*)showthread\.php" />
<action type="Rewrite" url="viewtopic\.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
However, it does not able to redirect to the viewtopic.php. Any suggestion?
I have to enable httpRedirect in the IIS. Then I used this redirect rule in the web.config at the root of my forum:
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="*showthread.php" destination="/viewtopic.php?$P" />
</httpRedirect>

IIS7.5 URL Rewrite rule to perform a 301 redirect from mysite.hosting.com to mysite.co.uk

I use IIS 7.5
I have a website wich has a valid host like:
A) mysite.co.uk
and a DEFAULT host (using for testing proposes provided by the hosting company):
B) mysite.hosting.com
Website is visible on both address, creating a DUPLICATE CONTENT issue for Search Engine.
I need redirect all the traffic (for all pages) from B to A using a 301 redirect.
IIS7.5 Http Redirect it is not design for this situation so I suppose to use IIS 7.5 Url Rewrite Module.
My questions: how write the ROLE in my web.config? Thanks
Try adding something like this between the <System.webServer> tags in your web.config:
<rewrite>
<rules>
<rule name="Redirect mysite.hosting.com to mysite.co.uk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mysite.hosting.com" />
</conditions>
<action type="Redirect" url="http://mysite.co.uk/{R:0}" />
</rule>
</rules>
</rewrite>
Alternatively, you can do this using global rules by adding:
<rewrite>
<globalRules>
<rule name="Redirects to mysite.co.uk" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="mysite.hosting.com$" />
</conditions>
<action type="Redirect" url="http://mysite.co.uk/{R:0}" />
</rule>
</globalRules>
</rewrite>

Resources