iis url-rewrite for no script name - iis

We would like to redirect IIS traffic with URL www.site1.com to www.site2.com. We do not want to redirect traffic for url's with either folder names and or script names. In other words we do NOT want to redirect a URL like www.site1.com/someFolder or www.site1.com/somePage.php, but if the URL is "plain" www.site1.com we want to redirect to www.site2.com.

You can try below rule:
<rule name="Redirect 1" enabled="true" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAll">
</conditions>
<action type="Redirect" url="http://localhost:820/" appendQueryString="false" logRewrittenUrl="true" />
</rule>
you can set redirect URL based on your requirement

Related

URL Rewrite rule for a specific domain

URL Rewrite rule for a specific domain
if url is https and have this domain only
https://myServer/SomeApplication/
Redirect it to
https://myServer.mycompany.com/SomeApplication/
Added below didn't work in iis 10, windows server 2019
<rule name="httpsRedirect2" enabled="true" stopProcessing="true">
<match url="^myServer/(.*)" ignoreCase="true"/>
<action type="Redirect" url="https://myServer.mycompany.com/SomeApplication/" appendQueryString="false" />
</rule>
Can someone explain what I've done wrong?
The Rule pattern only can get the URL string as an input, does not include the query string, thus it will not work properly.
Here is an official explanation of how certain parts of the URL string can be accessed from a rewrite rule.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule
We can match the hostname in the Rule Condition section then apply the Rule Action.
<rule name="Myrule2" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="localhost" />
</conditions>
<action type="Redirect" url="https://vabqia969vm/" />
</rule>
</rules>
</rewrite>
Feel free to let me know if the problem still exists.

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.

IIS rewrite rule not triggered when file-name plus extension is in the url

I am struggling getting an IIS rewrite rule working.
The old urls look like:
https://wwww.testserver.com/package.aspx?TrackingNumber=number
The new web app is expecting the urls in this format:
https://wwww.testserver.com/package/number
Currently I have the following rule setup, which is not working.
<rule name="Rewrite to new Package site" stopProcessing="true">
<match url="^package\.aspx\?TrackingNumber=([_0-9a-z-]+)" />
<action type="Redirect" url="https://www.testserver.com/package/{R:1}" logRewrittenUrl="true" />
</rule>
The interesting part is, if I remove package.aspx\? from the rule, urls like these https://wwww.testserver.com/oldwebsite/TrackingNumber=number are getting matched.
In my test calls I replaced package.aspx with package.html and these urls are not getting matched as well. It looks like IIS ignores urls with filenames in the url.
You could use below url rewrite rule to redirect https://wwww.testserver.com/package.aspx?TrackingNumber=number to https://wwww.testserver.com/package/number:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="package(.aspx)" />
<add input="{QUERY_STRING}" pattern="TrackingNumber=(.*)" />
</conditions>
<action type="Redirect" url="http://localhost:2568/package/{C:1}" appendQueryString="false" />
</rule>
Note: You could modify hostname as per your requirement.
Regards,
Jalpa

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