In IIS 8.5, how do I map a sub-domain to an application within a site? - redirect

Currently I have an IIS 8.5 server with a URL of:
subdomain.domain.com/application_instance_name_1
Under a site called Portal Application there are 3 instances of the application, one for each customer.
I would like to create a subdomain per customer and direct it to their version of the application under the Site called Portal Application, for example, Customer 1 would type in:
customer_1_name.domain.com
and in IIS it would be directed to the site and Application below:
Sites/Portal Application/customer_1_application
This is my first time posting on here, I did find something that maybe relevant here but its for Apache not IIS.
IF you need further information please let me know.
Thanks
brian

1) You need to install URL Rewrite and ARR module for IIS
2) Enable ARR. On the Application Request Routing page, select Enable proxy
3) Create rewrite rule
<rewrite>
<rules>
<rule name="rewrite customer_1_name.domain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^customer_1_name.domain.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="http://subdomain.domain.com/Portal Application/customer_1_application/{R:1}" />
</rule>
</rules>
</rewrite>
P.S. You might have problem with your resources(images,styles,css,js). Because your html might contains absolute paths to resources
You can check this post, when author is creating outbound rule for fixing relative urls https://blogs.iis.net/carlosag/setting-up-a-reverse-proxy-using-iis-url-rewrite-and-arr

Related

What is wrong with this rewrite rule

I am attempting to create a redirect rule to direct all traffic to one of my pages on my old site to my new site. I have created this rules in the parent web.config on my old IIS server:
<rule name="redirect to survey domain" stopProcessing="true">
<match url="takesurvey" />
<conditions>
<add input="{HTTPS}" pattern="^emory.zanek.net$" />
</conditions>
<action type="Redirect" url="https://survey.zanek.net/{R:1}" redirectType="Temporary" />
</rule>
Essentially I want to redirect all traffic for the specific page takesurvey to a new survey domain. So if the user navigates to:
https://emory.zanek.net/goalw4/takesurvey.aspx?id=fasidisdf
it should redirect to:
https://survey.zanek.net?id=fasidisdf
However, When I applied the rule, and then I navigate to https://emory.zanek.net/goalw4/takesurvey.aspx?id=fasidisdf, it just hangs forever. What am I missing here?
You are specifying {HTTP_HOST} but both of your URLs are HTTPS which operates on a different TCP protocol.
For your simple scenario I would just change {HTTP_HOST} to {HTTPS} in your input line.
For more information about this and robust ways to implement more complex/multiple cross types, see the link below.
Web config rewrite HTTPS

Restrict requests by HTTP referer - IIS

I have an IIS website at mydomain.com/mywebsite which has a URL rewrite request that for any requests to a 'maps' path (mydomain.com/mywebsite/maps) through the website, it will redirect requests to an external site to retrieve mapping image tiles that display on the page.
If someone tries to go to mydomain.com/mywebsite/maps on their browser, I do not want it to be accessible as I want to restrict these requests only to the website.
How can I set up a restrict rule in IIS to configure this?
You can try to create a request blocking rule for this, here a example for you as reference:
<rule name="RequestBlockingRule1" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URL}" pattern="^maps$" />
</conditions>
<action type="AbortRequest" />
</rule>
I have managed to restrict access to the URL path by adding an HTTP_REFERER condition in my URL inbound rewrite rule so it only accepts requests from my website domain which seems to work.
<conditions>
<add input="{HTTP_REFERER}" pattern="*mywebsite.com*" />
</conditions>

301 Redirect one domain to another using web.config

I have multiple domains pointing to one hosting location.
I wish to establish one of the domains as my main domain and therefore I wish to perform a 301 redirect to this main domain whenever a user accesses my site from a secondary domain.
For example:
www.example.com
This is my main domain. I want all other domains associated with my site to redirect to here.
If a user comes in on:
www.test.com or
www.test.com/anypage
etc.
Then I want the user to be redirected to the example version of that page.
How do I do this using the web.Config file of my application? The reason I ask is that usually my web hosting provider has a tool in their back office that allows me to setup this redirect however, our client has opted for a different hosting provider that do not provide such a tool.
I have attempted to do this redirect using the following code but it doesn't seem to work:
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^test\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}}" redirectType="Permanent" />
</rule>
My application is an Umbraco powered site and so has several system.webServer entries in the web.config file. It may just be the case that I have entered this code in the wrong place but any help here would be greatly appreciated as I am only used to doing 301 redirects in .htaccess files.
This is not really that umbraco related but I think what you want to do is this:
<rewrite>
<rules>
<rule name="redirect" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Match all urls unless the host name part is exactly www.example.com - and redirect those to www.example.com/whatever.

Redirect DNN Portal Alias to a Specific Page of the Portal

I need to redirect requests to the root of a specific Portal Alias of a DNN 07.03.02 install to a page of that same portal. The redirection can only effect one portal alias, another portal alias on the same portal must not redirect the same. Also, the DNN install that hosts this portal has many other portals so I cannot force redirection via a meta redirect file in the site's root. I configured an IIS (8.5) rewrite rule, such as below, which technically does the redirection I want, but this breaks the relative path to all images and stylesheets which are located in the portal's root directory. I've also tried DNN SiteURL redirection rules but those fail to force any redirection at all. If any has any suggestions of how I can configure such redirection for a DNN portal I'd greatly appreciate it.
<rewrite>
<rules>
<rule name="my redirect" stopProcessing="true" enabled="false">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www.)?mysite.com$" />
</conditions>
<action type="Rewrite" url="/newpage" appendQueryString="true" />
</rule>
</rules>
</rewrite>
I corrected this issue by adding an exclusion to my IIS rewrite rule such as:
<add input="{PATH_INFO}" pattern="^/portals/0/" negate="true" />
so that the path to my portal's content would not be rewritten.

Advanced ASP.NET MVC routing scenario

I have an ASP.NET MVC app with the following deployment requirements:
The URL structure must be something like:
http://server/app/[enterprise]/[communinty]/{controller}/{action}/...
What I think I want to be able to do is intercept the URL before the MVC route handler gets its hands on it, remove the [enterprise]/[community] parts, and then allow MVC to continue processing as if the original URL had not contained those two segments.
Here's why:
The application exposes multiple portals to multiple customers (enterprises), and each community within an enterprise has its own user population. This kind of scheme could also be served by physically deploying one application instance (binaries,content,web.config) into each [community] directory, but for logistical and performance reasons, I don't think we want to go down this path. So I'm trying to virtualize it through routing tricks.
Any suggestions on how to go about this scheme, or alternate solutions would be appreciated.
We are on IIS 7, if that makes any difference.
You can use the following route before the default route
routes.MapRoute(
null,
"{enterprise}/{community}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can then ignore {enterprise} and {community} parameters in your action methods.
Here is a possible solution with IIS Rewrite module. It may not be the best approach, but it may work. Is there an easier/better option within the MVC routing? Not sure. Only just started doing that myself.
Using "http://server.com/app/enterprise/community/controller/action/" as an example.
What happens:
Strips the string out of the URL. New
URL:
http://server.com/controller/action/
Redirects the user to new URL. User's
browser now shows:
http://server.com/controller/action/
Takes the new URL and tries to
rebuild it to grab the correct
content. User's browser shows:
http://server.com/controller/action/ ;
IIS returns:
http://server.com/app/enterprise/community/controller/action/
All of this would be in the web.config once the IIS rewrite module is installed:
<rewrite>
<rules>
<clear />
<rule name="Redirect to remove Offending String" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="server.com/app/enterprise/community*" />
<action type="Redirect" url="/{R:1}" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_NAME}" pattern="*server.com*" />
</conditions>
</rule>
<rule name="Rewrite to get Original Content" enabled="true" patternSyntax="Wildcard" stopProcessing="false">
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_NAME}" pattern="*server.com*" />
</conditions>
<action type="Rewrite" url="app/enterprise/community{R:1}" />
</rule>
</rules>
</rewrite>
Note: Just did this quick, haven't tested.