Multiple Country/Language Sites Under Single Web Application - TLD Routing Rewrites - asp.net

I want to host multiple Top Level Domains (TLDs) off of the same web application.
Scenario: www.mywebsite.com has language sub-folders of /en-us/, /en-gb/, /fr-ca/, /ja/, etc...
So www.mywebsite.com/en-gb/ would be the UK version of the site.
UK users should go to www.mywebsite.co.uk but be routed to www.mywebsite.com/en-gb/
In IIS, I've set the bindings for this web application to handle both www.mywebsite.com and www.mywebsite.co.uk domains.
The URL Rewrite 2.0 module is added to IIS and includes this rule:
<rewrite>
<rules>
<rule name="CanonicalHostNameRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mywebsite\.co\.uk$" />
</conditions>
<action type="Rewrite" url="http://www.mywebsite.com/en-gb/{R:1}" />
</rule>
</rules>
</rewrite>
Users who go to www.mywebsite.co.uk have the URL rewritten to www.mywebsite.com/en-gb/, however, I want the URL to remain www.mywebsite.co.uk for them, and in fact, I would want the www.mywebsite.com/en-gb/ to be rewritten to www.mywebsite.co.uk for consistency.
I'm still not even quite sure what the proper terminology is for what I want to do. So far I've run across 'multi-tenancy', 'application request routing', 'URL routing', 'URL rewriting', and a few others.
Here are a few resources I've been reading to try and figure out how best to handle this. Am I on the right track? I haven't found a good example that demonstrates doing this with TLD's.
Scott Forsyth - Multiple Domains Under One Site
Scott Guthrie - URL Routing With ASP.NET 4 Webforms
Stackoverflow - MVC Routes Based On A SubDomain

Related

Using IIS rewrite rules to rewrite from main website to sub application

We have an ASP.NET Web API application (.NET 4.*) which is hosted as a website in IIS. The ASP.NET application contains server-side rendered pages, but also a REST API which is available at /api/v1.
A new API is in development, which is using ASP.NET Core (.NET 7) and will be hosted as an IIS application (believe me, we cannot get around that for now) under the main website (which contains the ASP.NET Web API application).
The new API should also be available on /api, but since this is an application under the main website, if we would use /api for the new application, the REST API of the main website would not receive requests anymore. Hosting it under /api/v2 is not possible as IIS applications cannot contain a / in the alias (and we also do not prefer a version in the path anymore).
Instead, we would like to create the application using another name, /foo for example, and use rewrite rules in the web.config to rewrite from the main website to the new application, eg.:
<system.webServer>
<rewrite>
<rules>
<rule name="Foo">
<match url="^api\/((?!v1).*)$" />
<action type="Rewrite" url="foo/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
But this doesn't seem to work. It looks like the request is rewritten, but still handled by the main website, not by the sub application. Is it not possible to rewrite (not redirect!) a request from a website to a sub application within that website? If not, any other suggestions to host both applications on the same starting path (it should remain two separate applications)? At the moment, there is no possibility for a reverse proxy which could solve this issue.
If you want to access requests starting with /api by typing /foo in the browser address bar, you can try the following rule:
<system.webServer>
<rewrite>
<rules>
<rule name="Foo">
<match url="foo(.*)" />
<action type="Rewrite" url="api{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>

Request for HTTP gets logged as HTTPS and won't redirect

Man I have been struggling with this for a long time. I really hope someone can help me figure this out.
Windows Server 2012 R2
IIS 8.5
Using URL Re-Write:
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Pretty standard stuff. Here's where it get's weird:
The rule above is in place on the parent site. The redirect works MOST of the time on the parent site, but with some pages (and it's consistent) it will JUST. WILL. NOT. redirect them to HTTPS.
That parent site also has several sub-sites (web services, api's, others, etc). And on the sub-sites, NONE of the pages will redirect, even though it would appear as though the rule that was put in place on the parent site got inherited by all of the sub-sites.
Ok, and this is where it gets even more weird:
Using this excellent tutorial, I set up Failed Request Tracing. I then promptly tried to load one of the pages that is not redirecting by loading the page in a browser NOT on the web server. Example:
HTTP://v10staging.mydomain.com/some-sub-page
NOTE: I requested HTTP and not HTTPS. Then, I took a look at the Failed Logs, and this is what shows up:
Although I requested HTTP, it actually got logged as HTTPS.Tell me if I'm wrong, but it looks like the server thinks the request is already at https, and that's why it's not being redirected.
What. Is. Going. On? And how do I fix it?

Disabling port 80 on an asp.net vnext website on azure

I have a feeling this should be easy, but I'm struggling to find out how to do it.
I have a website that I want to restrict to HTTPS. It is an asp.net vnext website (not mvc) that is deployed to Azure. It is serving up static files without going through the ASP.NET pipeline.
In previous versions of asp.net, you could add system.webServer rules to do a redirect. This is gone from vNext. If I was using Mvc I could use the RequireHttps attribute or I could write custom middleware to do the redirect, but this only kicks in when the asp.net pipeline is activated. My html and js (it's a SPA app) would still be served up. If I was deploying to IIS instead of Azure, I could configure it there.
So, how do I tell an azure website to only respond on port 443 without a web config file?
According to Azure Documentation you can add a web configuration file for applications written in any programming language supported by Azure (Node.js, PHP, Python Django, Java). You can find detailed information here.
Here is a sample:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Force HTTPS" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
You can achieve the redirect by adding the web.config file to your deployment. The documentation says:
when hosted on Azure App Service- Azure creates the file automatically during deployment, so you never see it. If you include one as part of your application, it will override the one that Azure automatically generates.
for additional level of security probably you can use nsg. Azure documentation says NSG as "you can use Azure network security group to filter network traffic to and from Azure resources in an Azure virtual network".
You can try using cli to create a nsg and put rule in there to block traffic to port 80.

Single WebApplication with multiple domain names

For example, I have a Web Application www.mywebsite.com based on classic ASP.NET and IIS 7.5. Now I registered another domain name www.mywebsite.cc. In my Web Application I want to create subfolder /cc and somehow transparently rewrite all requests from www.mywebsite.cc/something to www.mywebsite.com/cc/something. Why I need this? I want both websites to share same static variables, cache, database connections etc. Please point me what technology I must dig in order to implement what I need.
There are a couple of ways to do this, you might try URL rewriting. Your code might look something like this, but you'll need to adjust the line following string: "^something/?$|^something/(.*)$" to contain the proper match code. This should get you started and hopefully someone else will be able to comment on the proper match code.
<rewrite>
<rules>
<remove name="RedirectToUtility"/>
<rule name="RedirectToUtility" stopProcessing="true">
<match url="^something/?$|^something/(.*)$"/>
<conditions/>
<serverVariables/>
<action type="Redirect" url="http://www.mywebsite.com/cc/something/{R:1}"/>
</rule>
</rules>
</rewrite>
http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module

Redirect *.domain.com & domain.com to www.domain.com

I've got an Search Engine Optimisation problem where users are able to access my site by specifying any sub-domain. This is causing duplicate page issues with SEO.
For example if a user mis-types 'www' then posts a link on a forum, google is crawling 'wwww.domain.com'. Furthermore, google is also crawling 'domain.com'.
I need a way of forcing the site to always redirect to 'www.domain.com' regardless of how the user accesses the site.
Is it possible to do this in the web.config file? If not, how else can I achieve this?
Cheers, Curt.
You can do this using the IIS URL Rewrite module. Here's the config to rewrite domain.com to www.domain.com:
<system.webServer>
<rewrite>
<rules>
<rule name="Canonical host name">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
You might need to modify the regex a bit or add a second rule to support rewrite of specific subdomains as well.
Unfortunately URL Rewrite module is not available on IIS6.
If you want to use url rewriting, you could check one of the following:
http://www.isapirewrite.com/ (not free)
http://urlrewriter.net/ (free)
http://cheeso.members.winisp.net/IIRF.aspx (my favourite)
Ah, just did this!
Set the default site to just redirect all calls using URL Redirect to your www.site.com, then create another site with your actual content that binds just to the www subdomain.
This will mean that all traffic will be redirected to the www site if there is no other binding available.
This has worked perfectly for me:
IIS 6 how to redirect from http://example.com/* to http://www.example.com/*
I had to change a bit of the code to get it to work with my Url-Rewriting, but apart from that, spot on!

Resources