IIS Rewrite rule for a subfolder path - iis

i am hosting a couple of websites on different servers and i am using IIS 10 + url rewrite on the frontend server as a reverse proxy only.
the redirection is as follows :
www.example.com rewrite to server A :
siteB.example.com rewrite to server B :
siteC.example.com rewrite to server C :
This scheme allows me to only have one certificate to manage for all my subdomain websites
This is working fine on subdomains with conditions matching rules.
Now i want to do a rewrite rule from a sub directory to map the entire site D to that subdirectory.
example:
www.example.com/siteD rewrite to server D :
the site redirects the first page but the aspect look weird and links are not functioning.
is it the right way to handle that with conditions {C:2} or should the pattern be applied to the match url ?
here is my web.config :
<rule name="www.example.com_site_d" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
<add input="{REQUEST_URI}" pattern="^(/siteD/)(.*)$" />
</conditions>
<action type="Rewrite" url="http://myinternalsited:80/{C:2}" logRewrittenUrl="true" />
Thanks

As far as I know, the REQUEST_URI means the entire URL path with querystring.
If the REQUEST_URI is /siteD/test?q=IIS+url+rewrite, the result of C:2 is:test?q=IIS+url+rewrite.
Result:
If the REQUEST_URI is /siteD?q=IIS+url+rewrite,the condition will not be matched.

Related

How can i include a page in an IIS url rewrite rule (redirect)

I'm fairly new to url rewrite and trying to figure this thing out.
I have a website with the following structure:
http://localhost/virtualdirectory1/somepage.aspx?parameter1=x
http://localhost/virtualdirectory2/
Now I added a rule for a redirect (inbound rule):
matches pattern (regex) ^$ to make sure that if anybody goes to http://localhost that they are redirected to http://localhost/virtualdirectory1/somepage.aspx?parameter1=x
This also works fine for http://localhost/?paramter1=x which gets redirected properly.
This still allows me to approach http://localhost/virtualdirectory2/ directly.
However if the user goes to http://localhost/somepage.aspx?paramter1=x they are not redirected to the virtualdirectory1
Could give me some tips on how that can be done?
Thanks!
Is this rule achieve your requirement? It will redirect both http://localhost and http://localhost/somepage.aspx?paramter1=x but it won't redirect http://localhost/.
<rule name="redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^(/somepage\.aspx)?$" />
</conditions>
<action type="Redirect" url="virtualdirectory1/somepage.aspx?parameter1=x" appendQueryString="false" redirectType="Temporary" />
</rule>

Always redirect from any domain prefix to www.example.com with iis url rewrite rule

we have a site at something.example.com and we want it to go always redirect to www.example.com
We already have the rule in for 'adding' www. when a user just puts example.com.
But we have a test url that is in place for other reasons and we want the live site to ALWAYS redirect to www.example.com no matter what prefix it has before the domain.
just use this regex .* in <match url="" /> or IIS URL re-write pattern field
for additional information:
dot means any single char
Asterix means zero or more of a char
so .* means zero or more of any char
According to your description, I suggest you could try to use {http_host} to match the domain part in the url rewrite rule.
Details, you could refer to below rules:
<rule name="rediect" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(.*).example.com" />
<add input="{HTTP_HOST}" pattern="^example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>

IIS is serving two versions of the same folder if virtual path is typed case insensitive

If the user types the virtual path wrong such as test.com/user instead of test.com/User. IIS will return both /user and /User. I am lost on how to prevent this. The result i would like would be for it to return everything under /User regardless if typed /user.
*Note: In terms of this example I only have /User listed in IIS.
I have url rewrite installed in my iis, but i am having no luck
According to your description, I suggest you could try to use below url rewrite rule to achieve your requirement.
Details Url rewrite rule as below:
</rule>
<rule name="Prevent" stopProcessing="true">
<match url=".*" />
<conditions >
<add input="{URL}" pattern="user" />
</conditions>
<action type="AbortRequest" />
</rule>

IIS Rewrite everything after the domain

I have to domains - a.com and b.com. I want to use iis rewrite so that anything going to www.a.com/ will be redirected to www.b.com/index.asp?i=.
For example www.a.com/XXX will be redirected to www.b.com/index.asp?i=XXX
I put (.*?) in the pattern. The problem is when I enter http://www.b.com/index.asp?i={REQUEST_URI} as a redirect action, the redirect url includes a slash. For example www.a.com/XXX is redirected to www.b.com/index,asp?i=/XXX instead of to i=XXX. Is there any way to get rid of the slash?
You could use below url rewrite rule:
<rule name="test1" enabled="true" stopProcessing="true">
<match url=".+" />
<conditions>
</conditions>
<action type="Redirect" url="http://www.b.com/index.asp?i={R:0}" />
Regards,
Jalpa

redirect subdomains from one domain to another domain's subdomain in iis with url rewrite

I want to redirect subdomains that come to iis to subdomains in localhost:port that my dotnet core application is running.
Simply i want to redirect like this:
xyz.example.com => xyz.localhost:4000
john.example.com => john.localhost:4000
test.example.com => test.localhost:4000
example.com => localhost:4000 (no subdomain address redirects to localhost:4000)
How can I do this with url rewrite rules is iis?
Edit:
there are some solutions for one subdomain (like this question) but my question is more general to redirect all subdomain from one domain to corresponding subdomain in another domain.
Here is the rule that performs redirect you are asking for:
<rewrite>
<rules>
<rule name="Test Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^((.+.)?)example.com" />
</conditions>
<action type="Redirect" url="http://{C:1}localhost:4000{REQUEST_URI}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
It's pretty straightforward:
<match url=".*" /> matches any URL. Whether requested URL is in example.com domain is checked in later condition. match section is useless for our case because it operates with the URL part without a hostname (after first '/').
Condition <add input="{HTTP_HOST}" pattern="^((.+.)?)example.com" /> matches domains like xyz.example.com or example.com. If condition is matched, capture group {C:1} will contain xyz. for xyz.example.com and empty string for example.com, which will be used further.
<action type="Redirect" url="http://{C:1}localhost:4000{REQUEST_URI}" appendQueryString="false" />
The result URL is built as http://{C:1}localhost:4000{REQUEST_URI}. {C:1} will hold subdomain name as said above and {REQUEST_URI} contains URL part after hostname. We also set appendQueryString to false because query is a part of {REQUEST_URI}.
You should add this <rewrite> section to site web.config under <system.webServer>. You could also configure it manually from IIS Configuration Manager. Here is a screenshot of such configuration:
Finally, here is a set of test that demonstrates how urls are changed:
[TestCase("http://xyz.example.com", "http://xyz.localhost:4000/")]
[TestCase("http://xyz.example.com/some/inner/path", "http://xyz.localhost:4000/some/inner/path")]
[TestCase("http://xyz.example.com/some/inner/path?param1=value1&param2=value2", "http://xyz.localhost:4000/some/inner/path?param1=value1&param2=value2")]
[TestCase("http://example.com", "http://localhost:4000/")]
[TestCase("http://example.com/some/inner/path", "http://localhost:4000/some/inner/path")]
[TestCase("http://example.com/some/inner/path?param1=value1&param2=value2", "http://localhost:4000/some/inner/path?param1=value1&param2=value2")]
public void CheckUrlRedirect(string originalUrl, string expectedRedirectUrl)
{
using (var httpClient = new HttpClient(new HttpClientHandler {AllowAutoRedirect = false}))
{
var response = httpClient.GetAsync(originalUrl).Result;
Assert.AreEqual(HttpStatusCode.MovedPermanently, response.StatusCode);
var redirectUrl = response.Headers.Location.ToString();
Assert.AreEqual(expectedRedirectUrl, redirectUrl);
}
}

Resources