How to create a virtual subdomain in asp.net? - c#

I’ve wanted to use subdomains for sub-sites within a single application for a while now. Basically, instead of setting up multiple sub-sites like this…
http://example.com/shop1.aspx?name=john/
…I wanted this…
http://john.example.com/
I want dynamic subdomain virtually using URL rewrite
I have no idea how to do this.
-----------UPDATE------
After searching over the internet I got following code.it redirect perfect but not rewriting the URL
my page is- http://domain/mySpace.aspx?una=sunil
<rule name="Redirect to Subdomains" stopProcessing="true">
<match url="^mySpace.aspx$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^una=(.+)$" />
</conditions>
<action type="Rewrite" url="http://{C:1}.{HTTP_HOST}"
appendQueryString="false" />
</rule>
But still not able to resolve it.

Related

If user is coming from domain testx.com, rewrite url to mydomain.com/testx

I have an ASP.Net website, but several domain names are pointing to the same website.
so for example lets say i have following domains
www.myMainDomain.com
www.mySite1.com
www.mySite2.com
I have 1 actual website with all the pages and subpages i want to have.
www.myMainDomain.com
www.myMainDomain.com/site1
www.myMainDomain.com/site1/products
www.myMainDomain.com/site2
www.myMainDomain.com/site2/products
...
So i would like to have a rewrite rule in my web.config
so that if the user is coming from the www.myMainDomain.com, it is just the normal site as it is existing
but if they are coming from www.mySite1.com, the rewrite rule should kind of take them to www.myMainDomain.com/site1, and if they go to a subpage, like products, it is always rewritten so like this
www.mySite1.com -> www.myMainDomain.com/site1
www.mySite1.com/products -> www.myMainDomain.com/site1/products
I have something like that, but this is not working, and it is actually causing a 500 Error
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/site1/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
What could be a correct solution?
I think you could use the inverse of standard canonicalhostname redirect with some changes:
<rule name="CanonicalHostNameRule_Mod" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.mySite1\.com$" />
</conditions>
<action type="Redirect" url="http://www.myMainDomain.com/site1/{R:1}" />
</rule>
In this way you have to make a rule for each secondary domain.

write a redirect rule in web.config in asp.net application

I have a help section on a web site, which for now I want it to redirect to another place. The rest of the site should stay the same, which means that only the help section/content should make the user go to another site:
device.domain.net/collection/hgkhghj should return to device.domain.net/collection
I can have anything at the place of hgkhghj . If i am writing device.domain.net/collection then it should return device.domain.net/collection
<rule name="Help Redirect" stopProcessing="true">
<match url="(.*)/collection(.*)" ignoreCase="true" />
<action type="Redirect" url="http:/device.domain.net" appendQueryString="false" redirectType="Permanent" />
</rule>
but currently it is returning to device.domain.net.
I want to make one more Rule in which if i enter device.domain.net/gcfhgg then it should return to device.domain.net.
<rule name="Help Redirect" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<action type="Redirect" url="http://device.domain.net" appendQueryString="false" redirectType="Permanent" />
</rule>
But it is not working.
For your first rule you could use the following (assuming it is on the same domain):
<rule name="Help Redirect" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^collection/(.*)" />
<action type="Redirect" url="collection" appendQueryString="false" redirectType="Permanent" />
</rule>
If this is on another domain you will need ARR installed as well. Your second rule is more complicated as it looks as if you are attempting to perform a redirect only if you hit a 404 page. This is something I have never done using rewrite rules. Is there a reason for doing this as opposed to showing a custom 404 page?

Redirecting an asp.net MVC URL to a specific controller/action/parameter using httpRedirect in web.config

I have a website hosted in Azure, which will be accessed with the URL https://abc.cloudapp.net/controller1/action1/parameter1
My Customer wants the following.
When we try to access the url "https://abc.cloudapp.net", it should automatically redirect me to the actual url mentioned above for the website.
I want this to be achieved using the web.config. and I tried several combinations (Couple of those are listed below).
Combination 1:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to Full URL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URL}" pattern="^abc.cloudapp.net$" />
</conditions>
<action type="Redirect" url="https://abc.cloudapp.net/controller1/action1/parameter1" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Combination 2:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to Full URL" stopProcessing="true">
<match url="https://abc.cloudapp.net" />
<action type="Redirect" url="https://abc.cloudapp.net/controller1/action1/parameter1" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
None of these combinations have worked.
I had posted a workaround below for this problem.
However, I need some quick help in this for a permanent solution.
I still need help on this, can some one help please.
I had found a workaround for this.
When we use the url "https://abc.cloudapp.net", using the default route config we can set this to go to action1 in controller1.
Then in "action1" of "controller1" if we see that the "parameter1" is null (must be delared as a nullable type), we need to initialize the parameter1 to a known value and redirect to the same "action1" in "controller1"
public ActionResult Index(int? parameter1)
{
if(string.IsNullOrEmpty(parameter1)
return RedirectToAction("action1", "controller1", new {#parameter1 = "knownValue"});
else
{
//whatever action you wanted to do based on value in parameter1
}
}
However this whole method calls for hard coding, We are assigning a know value to the paramter1. If there is a change in this value, we need to change the code
Hence I do not recommend this. However for now I have used this workaround. I still need a proper solution around this problem.
MY R & D Continues... ... ... ... ...

URL Rewrite on IIS always fail when change the pattern

I'm trying to work in a rewrite problem, but its my first time and I guess I'm doing something very wrong.
A few users of our portal, try to navigate typing https://www.dudalina.proveagora.com instead the right one https://dudalina.proveagora.com. We have the SSL only for *.proveagora.com, so I started to try a redirect on the users who type the wrong url.
Here, the print of rewrite working great:
but, some users type https:// and some users type http://. By this way, I've tried to change the pattern.
I dont know why, but using the second pattern, the url https://www.dudalina.proveagora.com doesnt redirect anymore.
Just to explain a little more, the dudalina part is dynamic too. Dudalina is the store name, and it will change accordingly with store. So, we can have latter something like http://bestbuy.proveagora.com or anything else and this patterns much works with all the options.
I know, the user cant type www.bestbuy.proveagora.com but they do, and we need to make it works! :(
Any help?
EDIT
xml Web.Config
<rewrite>
<rules>
<rule name="RemoveWWWPrefix" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(https\:\/\/|https\:\/\/)?([a-zA-Z]+\.)?(proveagora\.com){1}(\/.*)?$" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" url="http://dudalina.proveagora.com" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Here you go:
<rule name="RemoveWWWPrefix" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(.+\.)proveagora\.com$" />
<add input="{HTTP_HOST}" pattern="^dudalina\.proveagora\.com$" negate="true" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="https://dudalina.proveagora.com" appendQueryString="false" />
</rule>
What it will check is that the protocol used is HTTPS and that the subdomain of proveagora.com is present and different from dudalina.
If that's the case, it redirects to https://dudalina.proveagora.com

Redirect https to http using rewrite rule in webconfig file

This is what I have tried so far.
<rule name="https main site to http" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="Off" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{REQUEST_URI}" />
</rule>
How can I redirect https://www.mysite.com to http://www.mysite.com
This was asked a long time ago, but it looks like this has already been answered here: How to force HTTPS using a web.config file. Pay close attention to one of the comments that mentions that the query string is appended twice if you use the full answer.

Categories