IIS url rewrite rules to hide subfolders - asp.net

I have 2 .net API projects, servce1 and service2.
I would like to put them both under the same domain (site). I can ensure the APIs routing not overlapping.
I deployed the projects as:
Default Web Site/
service1 (raw url: http://mydomian/service1/api/ONE)
service2 (raw url: http://mydomian/service2/api/TWO)
What I want to achieve is that I can use http://mydomain/api/ONE or http://mydomain/api/TWO to access both service1 and service2 APIs without the subfolders in the URL.
I tried using URL rewrite module I got it works for one, but I can not get the second one work, with the same settings. See the setting file below.
I tried moving the rules orders. no luck.
<rewrite>
<rules>
<clear />
<rule name="first" stopProcessing="false">
<match url="(.*)ONE(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URL}" pattern="(.*)/api/ONE(.*)" />
</conditions>
<action type="Rewrite" url="/Servuce1/api/ONE{R:2}" logRewrittenUrl="true" />
</rule>
<rule name="seconds" stopProcessing="false">
<match url="(.*)TWO(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="(.*)/api/TWO(.*)" />
</conditions>
<action type="Rewrite" url="/service2/api/TWO{R:2}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
What is wrong with my settings?
What are the alternatives?

Should the stopProcessing be set to "true", so that the second rule is not applied when the first rule gets matched?

Related

IIS Redirect HTTP to HTTPS and WWW to non-WWW

On IIS 10 with URL Rewrite Module 2.0 I need 2 rules
1) HTTP to HTTPS
2) WWW to non-WWW
First one created by Blank rule template.
For second one I use Canonical domain name template.
In my web.config rules likes like this:
<rewrite>
<rules>
<rule name="ForceHttps" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^cooltechunder\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://cooltechunder.com/{R:1}" />
</rule>
</rules>
</rewrite>
All cases works fine expect one starting with: http://www.
See this image for results I have:
https://i.imgur.com/h2l3Yw6.png
You wrote stopProcessing="true".
This means that, if the rule matches, subsequent rules will be skipped.
From the documentation:
A rule may have the StopProcessing flag turned on. When the rule action is performed (i.e. the rule matched) and this flag is turned on, it means that no more subsequent rules will be processed and the request will be passed to the IIS request pipeline. By default, this flag is turned off.
It seems to me that this is the situation you are describing that you did not want.
So, remove it.
Ok, My problem was different and related to bindings.
I have to specify 'Host Name' in bindings, as specific ports used by other websites also
And I forgot to add 'www' versions of bindings also.
Now my bindings looks like this: https://i.imgur.com/Lhdv4nS.jpg
Also I have changed rewrite code to more compact one:
<rewrite>
<rules>
<rule name="Https and non-www">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^cooltechunder\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://cooltechunder.com/{R:1}" />
</rule>
</rules>
</rewrite>

Redirect rule http to https not working when using full url

My azure cloud service has both http and https enabled in config files.
I tried adding this rule in my web.config
<rule name="RedirectHTTPToHTTPS" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
if I use http://mysite.kom ,it is correctly redirecting to https://mysite.kom
But if I type http://mysite.kom/other/default.aspx it is redirecting to http://mysite.kom/other/default.aspx instead of https://mysite.kom/other/default.aspx
I added GlobalFilters.Filters.Add(new RequireHttpsAttribute()); in my application_start event.But still "http://mysite.kom/other/default.aspx" is not getting redirected to "https://mysite.kom/other/default.aspx"
I tried all the similar questions in stackoverflow.I even moved the this rule to top of my rules config. Any suggestions to make this working
To resolve this , I added
if (!HttpContext.Current.Request.IsSecureConnection && !HttpContext.Current.Request.Url.Host.Contains("localhost"))
{
Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"]
+ HttpContext.Current.Request.RawUrl);
}
to Application_BeginRequest event inside my global.asax.cs file
Here is what I use:
<rewrite>
<rules>
<rule name="redirect HTTP to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{WARMUP_REQUEST}" pattern="1" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
While you aren't preserving the query string, it still seems like you have everything necessary to get http->https redirection. That makes me wonder if browser caching is causing you problems? I have had the issue before where I didn't think it was working because I hit the site before enabling http->https redirection and it turned out to be my browser serving up the page from cache.
I wrapped this in a site extension, and it uses the above rewrite rule, so I know it works:
http://www.siteextensions.net/packages/RedirectHttpToHttps/

IIS Query string lost when using URL Rewrite rules

I am trying to get a site to work with a different URL, basically one is the real URL(fakesite.com) and the other is a Branded URL(NewFakeSite.com). So far ive managed to get it to work so that:
http://fakesite.com/uv/ExpressBranding/ExpressHome.aspx
and
http://NewFakeSite.com/uv/ExpressBranding/ExpressHome.aspx
Both go to the same site and are both able to work as functioning sites. The Problem comes with Querystrings, I cant seem to get the second site to pass along the Querystring so if I go to a URL with a Querystring like:
http://fakesite.com/uv/EnviSetup/Wizard/GeneralInformation.aspx?DashboardId=0
For the Second site it cant find the page and instead gives me:
https://NewFakeSite.com/uv/EnviSetup/Wizard/GeneralInformation.aspx
with no QueryString at all.As such I need to add the Querystring. My webconfig is below, its not just my code so certain pieces of it like the HTTP to HTTPS and adding uv were not written by me.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<remove name="Redirect to HTTPS" />
<remove name="Redirect to add uv folder to dotcom" />
<rule name="Redirect to add uv folder to dotcom" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{map add uv after dotcom:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<serverVariables />
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{SERVER_NAME}" pattern="opterra\.esightenergy\.com" negate="true" />
</conditions>
<serverVariables />
<action type="Rewrite" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" />
</rule>
<rule name="AddQueryStringForENVIDashBoards">
<match url="https://prod.utilityvision.com/uv/EnviSetup/EnviDashboardList.aspx(\?(.*))" />
<action type="Rewrite" url="HTTPS://opterra.esightenergy.com/uv/EnviSetup/Wizard/{C:1}" appendQueryString="false" />
<conditions>
<add input="{URL}" pattern="https://prod.utilityvision.com/uv/EnviSetup/Wizard/(.*)" />
</conditions>
</rule>
</rules>
</rewrite>
<httpRedirect enabled="false" destination="" exactDestination="false" childOnly="false" />
</system.webServer>
</configuration>
Any Help would be greatly appreciated I am new to IIS so I hope this is even possible. Thanks!
EDIT: I have tried setting the appendQueryString="True" but still no Querystring. Thanks mjw.
Edit 2: The main goal is to make make it so the URL shows up as something different then it was originally programmed as, The problem is the Query String doesnt come with the rest of the new URL. I have added some code changes to see if this would fix the problem but it did not the URL remained the same. The code sample above is not the original I added this:
<rule name="AddQueryStringForENVIDashBoards">
<match url="https://prod.utilityvision.com/uv/EnviSetup/EnviDashboardList.aspx(\?(.*))" />
<action type="Rewrite" url="HTTPS://opterra.esightenergy.com/uv/EnviSetup/Wizard/{C:1}" appendQueryString="false" />
<conditions>
<add input="{URL}" pattern="https://prod.utilityvision.com/uv/EnviSetup/Wizard/(.*)" />
</conditions>
</rule>
You have server variable for query string so you can use it to append value to the redirection url:
QUERY_STRING
So for your request http://fakesite.com/uv/EnviSetup/Wizard/GeneralInformation.aspx?DashboardId=0
Important Server variable values are:
HTTP_HOST: www.fakesite.com
PATH: /uv/EnviSetup/Wizard/GeneralInformation.aspx
QUERY_STRING: DashboardId=0
Also if you open IIS->Url Rewrite and go to your rule you will see there is a option to test rule, you can simply paste your url and have an overview how URL is parsed. You will then also realize which part you can use to append query string.
http://www.iis.net/learn/extensions/url-rewrite-module/testing-rewrite-rule-patterns

How can I create a URL rewrite rule for a domain-only request?

I am trying to create a URL rewrite for a site that has multiple domains. For example one of the domains is mydomain.com and if a user puts www.mydomain.com in their browser and does not specify a page I want to rewrite the URL to call www.mydomain.com/landingpage.aspx?cat=1&sol=4.
If the user calls anything else such as www.mydomain.com/somepage.aspx this rule should be ignored.
I have installed URL Rewrite 2.0 on the server 2008 R2 machine we have and I have added this rule to the web.config.
<rewrite>
<rules>
<rule name="mydomain.com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain.com" />
<add input="{PATH_INFO}" pattern="^$" negate="true" />
</conditions>
<action type="Rewrite" url="\landingpage.aspx?cat=1&sol=4" />
</rule>
</rules>
</rewrite>
I am using the {PATH_INFO} of ^$ so that if anything other than just a call for the domain occurs this should ignore it I think. However it does not work.
I am using .NET 4.0 on the site.
Can someone tell me what I am doing wrong please?
You are looking for following rule:
This will check if URL is empty i.e. no page is specified using match url=^$ - empty string and redirect to specific page.
<rule name="Redirect to specific page" enabled="true" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain.com$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/landingpage.aspx?cat=1&sol=4" />
</rule>

url rewrite -> for non www to www - which is better and how? (site level or application level)

I'm doing url rewriting (Wildcards) for a sigle web site based on
this blog post. This is what I tried:
<rewrite>
<rules>
<rule name="Redirect example.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
These codes works perfect and we can add them manually to web.config of web site or use url rewrite in IIS.
My problem is I have many web sites (domains and subdomains - net , com , org) installed on my IIS and I have to do a repeated job for all of them!
Is it possible to use another way for redirecting non www to www (site level or application level) for all web sites? If application level is possible which configuration files should I change? Would you please show us the correct wildcards or regular expressions?
You may edit your applicationHost.config (int the %systemroot%\System32\inetsrv\config directory) so that it includes common url rewriting rules for your IIS installation; the following two rules (one for HTTP, the other for HTTPS requests, if needed) do exactly what you are after:
<system.webServer>
<rewrite>
<rules>
<rule name="NonWwwToWwwRedirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}:{SERVER_PORT}" />
</rule>
<rule name="NonWwwToWwwRedirectSecure" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="on" />
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.+)$" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}:{SERVER_PORT_SECURE}" />
</rule>
</rules>
</rewrite>
</system.webServer>
That being said, I think it is not possible to restrict a given set of rules to a specific application pool, however.

Resources