ASP.NET virtual subdomain via Rewrite - asp.net

I'm going to make pages accessible by subdomains like below:
http://product12.domain.com instead of http://domain.com/?productid=12
How can I do that? is it possible to create subdomains programatically via ReWrite or RouteMap?

Subdomains, by definition, are part of the DNS. You'd have to create the appropriate subdomains within your domain management system. I may be wrong, but I don't think URL rewriting is supposed to be used for host redirection; you'd have to either set up routes to catch your query strings and redirect to another host, or potentially use IIS to do it.

Related

DNS custom domain to subfolder

I have a SaaS app where every user has a personal subdomain: username.domain.com. Every user has a personal blog at username.domain.com/blog.
Now I want to accept custom domains, e.g. www.mycustomblog.com would be an alias for username.domain.com/blog.
If someone browses to www.mycustomblog.com/123, the page username.domain.com/blog/123 should be served.
However, I do NOT want a redirect. The user should still see www.mycustomblog.com/123in their address bar.
How can I achieve this behaviour? I have looked into Nginx reverse proxies, DNS CNAME records... but nothing seems to suit my needs. I can access both the custom domain DNS settings and all of the server's config files.
I think what you're looking for is a rewrite. However your described logic doesn't work:
www.mycustomblog.com -> username.domain.com/blog
appears to be missing a piece of identifying information on the left side. Perhaps www.mycustomblog.com/username? After that, it's just a matter of writing out the match/map statements to change the request to match what you've got on the server.

301 Redirect from app.com to www.app.com + always-https in an Azure Cloud Service with MVC app?

I have an MVC application being hosted in an Azure Cloud Service. I need to redirect all requests for:
app.com
http://app.com
sub.app.com
https:app.com
to
https://www.app.com
https://www.app.com
https://sub.app.com
https://www.app.com
respectively. Always ensuring a subdomain (or www), and always enforcing https. I can do this at the application level in something like global.asax, but this feels wrong. I'm currently using Amazon's Route53 to handle my DNS needs, though I'm not sure if DNS is the right layer for this concern either. What is the proper way to handle this?
HTTPS
Given that it is an MVC app you can enforce https by using the [RequireHttps] attribute. Personally I normally have a base controller that all my other controllers inherit from and I stick [RequireHttps] on that base controller.
Alternatively, register it as a global filter like so:
protected void Application_Start()
{
GlobalFilters.Filters.Add(new RequireHttpsAttribute());
//... other stuff
}
(copied from https://stackoverflow.com/a/3285179/11534)
Redirecting from the naked url
As far as redirecting from the naked subdomain to www you probably want to look at Url Rewriting, which you can stick in web.config. See Redirect all naked domain urls to subdomain(www) urls preserving the url, except for one page on IIS/ASP.NET for a very similar example.
I have to admit that I have not used the url rewriting a lot so maybe someone else can help with more specific details on how to achieve your specific requirement...

Keeping original HTTP_HOST when Redirecting/Rewriting

I am experiencing an issue with the IIS Rewrite Module and nopCommerce.
Situation:
I have several domains (Domain1.ca, Domain2.ca, etc.), each redirecting to a main domain (MainDomain.ca) at the Registrar level (using a CNAME record). Is it possible, using the IIS 8 Rewrite module, to retain the original HTTP_HOST value of the domain originally browsed to (Domain1.ca, Domain2.ca, etc.) instead of the redirected domain (MainDomain.ca)? I need this for the following reason:
In nopCommerce, each store corresponds to a unique domain (Store1 = Domain1.ca). To determine which store is active, nopCommerce obtains and resolves the "HTTP_HOST" value.
Unfortunately, since all domains get redirected to the main domain (MainDomain.ca), the original HTTP_HOST value (Domain1.ca) is lost. Therefore, not knowing which specific store needs to get activated, nopCommerce activates the first one in the list.
I assume that when a site has been redirected to, the original HTTP_HOST value is overwritten.
Does anyone have any experience with nopCommerce, HTTP_HOST, multi-store, domain redirection?
a CNAME does not perform any redirection.
A CNAME simply says that domain1.com will us ethe same dns records as domain2.com, aka it is an alias.
So if you are really are redirecting from your domain registrar then you are not using a CNAME record to do this. You must be using a REDIRECT service at the registrar, the most common one is an FRAME redirect, where they create a website for domain1.com and inside they put a frameset which points to domain2.com
I would suggest that remove this, and just use the CNAME only and then do the redirect at your webserver using URL REWRITE, this will then allow you to retain the original host name.

How to change URL address in ASP.NET?

I have a business requirement, where i should show a different URL in the address bar from the actual. Say for ex: I have hosted my site at Hum.com. But for some users, this URL should show up as CP.com at the address bar? Is it even possible?
The only way to do this is if you control both domains, hum.com and cp.com and if you configure your web server to serve the same application for cp.com and hum.com.
If above is the case (you control both domains), you can simply redirect the users to the appropriate domain using Response.Redirect.
This is easily done in Apache via NameVirtualHosts and I am sure IIS offers the same functionality.
Yes this is possible, but it's generally done at the DNS level and not within the application itself. You want the IP address of Hum.com to resolve to the same IP address as CP.com. This is how hosting sites such as Google Sites generally work.
To do this you need to own the DNS entry for your vanity domain name (i.e. CP.com) and you need to ensure that the hosting site is capable of associating requests for CP.com with the hosted website.
This can be done by redirect.
if(fUserOfCP && !HttpContext.Current.Request.RawUrl.Contains("cp.com/")){
Responce.Redirect(
HttpContext.Current.Request.RawUrl.Replace("hum.com/", "cp.com/")
, true);
}
This code is the idea, probably is better to break the RawUrl, check and reconstruct it on the redirect to avoid the existing of host on file name.
Assume that both names belong to you, and you have setup correctly the dns.
Rewrite is not possible on host name if this is your first thoughts.

Alternative to Response.Redirect to effect a subdomain

I have a site that is hosted in shared hosting environment. They use a wildcard subdomain setup and suggest using Response.Redirect to achieve the illusion of a subdomain.
Is there a way of doing this such that the "switch" takes place on the server rather than bouncing back down to the browser first?
Server.Transfer only works if I transfer to an actual resource. So redirecting from sub1.mydomain.com to www.mydomain.com/public/ does not work. I'd have to redirect to www.mydomain.com/public/mypage.aspx instead which i dont want to do.
To ensure that the "switch" takes place on the server, you could create a simple HTTP Module to intercept each request, inspect the requested URL and then forward them as needed . All your module has to do is handle the OnBeginRequest event, and then forward the request. In this way you could really have unlimited sub-domains.
Also might want add a blank host header, so that any requests for subdomains not listed get forwarded to the proper default website
If you aren't familiar with them, modules are very simple to create and work with.
Heres a link to a very similar implementation by Brendan Tompkins:
http://codebetter.com/blogs/brendan.tompkins/archive/2006/06/27/146875.aspx
You could also do some URL rewriting in the module should you need specific URL "look" behavior.

Resources