Single WebApplication with multiple domain names - asp.net

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

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>

Denying access to videos and audio directly from browser. Only thru main website

I have been browsing similar questions here but i haven't found my specific scenario.
I have domain.com. I have video.domain.com.
Now, before I continue, I am going to clarify. I am not trying to "protect" my video from being "stolen". This is virtually impossible.
I just don't want direct access to it in order to avoid leeching or downloads. My intention is to "protect" this video from unauthorized access (login and password will do for the website), but of course, if I don't do RTMP (which I intend to do) you got in your browser the full address of the video.
video.domain.com/course/1/Intro.mp4.
and that would not stop you from downloading it directly.
So, I tried a couple of things but didn't seem to work.
First, URL Rewrite. I am using IIS 7. I tried to block any request that {HTTP_REFERRER} was different than domain.com. Didn't work. Yes, it did block me from download it, but that's because my {HTTP_REFERRER} was empty. Which is also empty when domain.com try to access my subdomain. So, it did block the download but also to be able to play it from my website.
So, that, didn't work.
Next, I tried some IP Address Domain and Restrictions but didn't work either.
Has someone be able to do this successfully? I know amazon services are able to do this successfully but I can't think of a way.
Thank you
SOLUTION.
So, at the IIS level, in my videos.domain.com site, I created an URL Rewrite Rule where every request to a MP4 triggers this verification.
No empty Referer (if you call the video from video.domain.com/..../Intro.mp4, then it would not have a Referer).
Match ONLY "http://domain.com". If it doesn't match that, it would not serve the video. It would serve an image saying "Do not steal the videos" :)
Very simple.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ONLY from DOMAIN.COM" stopProcessing="true">
<match url="(?:mp4)$" />
<conditions>
<add input="{HTTP_REFERER}" pattern="^$" />
<add input="{HTTP_REFERER}" pattern="^http://domain\.com/.*$" negate="true" />
</conditions>
<action type="Redirect" url="http://videos.domain.com/leechingisbad.jpg" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
</system.webServer>

Configure ASP.NET Routing in Web.config

Is there a way to configure ASP.NET routing in the web.config file? I do not need anything fancy. If a user visits /myApp/list, I want to load /myApp/list.html. However, I do NOT want the address bar to change.
Is this possible? If so, how?
The best way is to use UrlRewrite module in IIS: http://www.iis.net/learn/extensions/url-rewrite-module
How you make this rule into web.config after installing UrlRewrite:
<system.webServer>
<rewrite>
<rules>
<rule name="my-first-url-rule" stopProcessing="true">
<match url="^/myApp/list$" />
<action type="Rewrite" url="/myApp/list.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
As of now there is No out-of-the-box way to configure routes in web.config file.
It seems vision was/is to add new routes on Application Start.
This doesn't stop us from creating custom configuration section and reading routes from there.
These are guesses why.
Most of the time, it seems, adding new routes could be prone to introduction of routing bugs and should be followed up with good regression testing. And therefore it probably will be done as part of new product release anyway. Perhaps that could be one the reasons why it's not configurable through configuration file.
Another reason could be that routes rules can be quite complex to put them into xml format - constraints, custom route handlers,etc.

hiding the path of a file on web server

I have a webpage that host documents on iis. When linking to those files for examples say I go to
www.webpage.com/Documents/testdocuments/innerfolder/1.pdf
I get the file back and have the complete url in the address bar. Is there a way to be able to browser to
www.webpage.com/Documents/testdocuments/innerfolder/1.pdf
, and get for example
www.webpage.com/Webdocs/1.pdf
as the url in the browser. My theory was make
www.webpage.com/Documents
a virtual directory, but I am not sure if I would still be able to access my files as
www.webpage.com/Documents/testdocuments/innerfolder/1.pdf
or if I would have to use for example
www.webpage.com/Webdocs/testdocuments/innerfolder/1.pdf
which defeats the purpose really. My ultimate goal is for the physical path of the file to not be visible to the user.
Any ideas would be greatly appreciated.
Thanks,
Since you are looking only to map one path to another you should use IIS url rewriting.
<rewrite>
<rules>
<rule name="Map Docs" stopProcessing="true">
<match url="^Webdocs/([_0-9a-z-]+)" />
<action type="Rewrite" url="Documents/testdocuments/innerfolder/{R:1}" />
</rule>
</rules>
</rewrite>
The other alternative is asp.net url routing. But it would be an overkill for what you need.

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

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

Resources