URL Rewriting help using IIS Rewrite - iis

Please help me redirecting my URL using IIS rewrite the following:
https://domain.com:8080/Context/services/.*
to
https://domain.com:8443/Context/services/.*
We only have 8080 port open and that's why need redirection for SSL.
But at the same time I don't want any other URLs getting redirected such as
https://domain.com:8080/Context/xyz (or anything except services)/.*
I tried the following but it is not working:
<rule name="HTTPS Request on 8080 Redirect to HTTPS Request on 8443 for Root"
patternSyntax="ECMAScript" stopProcessing="true">
<match url="https(.*)Context/services/.*" />
<conditions>
<add input="{SERVER_PORT}" pattern="8080" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}:8443/{R:0}" />
</rule>

Give this a try:
<rule name="HTTPS Request on 8080 Redirect to HTTPS Request on 8443 for Root"
patternSyntax="ECMAScript" stopProcessing="true">
<match url="^(Context/services/.*)" ignoreCase="true" />
<conditions>
<add input="{SERVER_PORT}" pattern="8080" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}:8443/{R:0}" />
</rule>
If you review the Accessing URL Parts section here, you will notice that the match URL contains everything after the root of the directory it is placed in. For something in the root directory as is your case, it will be everything after the hostname and port without a leading slash /.

Related

iis url rewrite not redirecting from default site to defaultsite/app

I am having trouble wrestling url rewrite and need to handle this in IIS. What I need is to ensure that all traffic that hits the server is redirected to https://server/app. I can handle this using a standard http to https rewrite which seems to work fine redirecting http://server to https://server/app but it doesn't work if someone hits https://server.
Here's my standard rewrite:
<rewrite>
<rules>
<rule name="http to https" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/app" />
</rule>
</rules>
</rewrite>

Redirect All Traffic in IIS 10 to another site

I am using URL rewrite to try and set up a rewrite from one site to the another site. I used URL re-write to set up this rule:
<httpRedirect enabled="false" destination="https://www.previdence.com" exactDestination="false" />
<rewrite>
<rules>
<rule name="SiteRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://www.previdence.com" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I have followed a couple of tutorials on setting this up and this is what should be set. So if I type in companyname.net it will redirect to https://www.companyname.com. If I type in https://www.companyname.net or https://companyname.net it goes to a 404.
Looking at the bindings for the old site there are bindings for PORT 443 and PORT 80 for www.companyname.net. I set the redirect in IIS for code 302, then I got URL rewrite as I explained above and I still get a 404 error.

URL Rewrite giving 404

I have subdomain with an A record to my IIS Server:
sub.domain.com
The IIS server has a binding for sub.domain.com and a cert for *.domain.com
What we need is when someone goes to sub.domain.com it takes them to another site but masks the URL and keeps the SSL. The destination page has in its SAN cert sub.domain.com.
Tried a forward on Godaddy which works but it doesn't keep the SSL and we get cert issues. So thought was to point to our server to pass SSL and then redirect it. I tried a URL Rewrite but it's giving a 404.
URL Rewrite:
<rewrite>
<rules>
<clear />
<rule name="Pay Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?sub\.domain\.com$" />
</conditions>
<action type="Redirect" url="https://www.domain2.com/cgsdesktop/PaymentLanding/UniversalPortal" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I just need to know how to get this done.
How about this:
<rules>
<rule name="Pay Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^sub.domain.com$" ignoreCase="true"/>
<add input="{HTTP_HOST}" pattern="^www.sub.domain.com$" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://www.domain2.com/cgsdesktop/PaymentLanding/UniversalPortal" appendQueryString="false" />
</rule>
</rules>
use sub.domain.com instead of sub\.domain\.com.
This article contains some useful recipes on IIS redirect/rewrite rules.
In order to mask the URL and keep the SSL in the browser bar, we need to create URL Rewrite action rules of URL Rewrite extension.
However, URL Rewrite action only supports to redirect the request to the same domain by default. We have to install the Application Request Routing extension when redirecting the request to another website.
https://www.iis.net/downloads/microsoft/application-request-routing
or we will get an Http 404 error.
See my preceding post for more details.
ASP.net URL Rewrite subdirectory to external URL
Feel free to let me know if there is anything I can help with.

Url Redirect using Regex only if no path in url

I want to write a url redirect rule inside my Web.Config such that redirect will happen only if there is no path in the url i.e.
if my url is https://www.webpagetest.org/forums/ then it should not redirect. But if it's only https://www.webpagetest.org, redirect should happen to google.com.
Web Server is IIS
This would check only the domain name -
<rule name="Rule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="www\.yourdomain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.google.com/{R:1}" />
</rule>

IIS Url Rewrite to another server

I have an old url www.mydomain.com/customer/1. I changed that server to ww2 and it displays fine at ww2.mydomain.com/customer/1. On my new IIS 8.5 www server how can I put in a rewrite rule so if the user goes to www.mydomain.com/customer/1 they will redirect to ww2.mydomain.com/customer/1. I have tried numerous patterns in the Url Rewrite 2.0 module and nothing seems to work. All other www requests I do not want redirected, those stay on my new www server.
I you want to redirect a single path, /customer/1, then you should add this to Web.config on the www.mydomain.com server:
<rewrite>
<rules>
<rule name="Redirect 'www.mydomain.com/customer/1' to 'ww2.mydomain.com/customer/1'" stopProcessing="true">
<match url="^customer/1$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mydomain\.com$" />
</conditions>
<action type="Redirect" url="http://ww2.mydomain.com{REQUEST_URI}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
If you wish to include additional paths, you can fiddle with the RegEx specified in the <match url="^customer/1$" /> tag.

Resources