subdomain url redirects to the main url - asp.net

I developed a website in asp.net and hosted on windows server which does not provide me the wild card entry for the subdomain name. How can I write my url as e.g
http://subdomainName.DomainName.org?
I want to redirect the url with a subdomain to the main domain; so url "subdomainName.DomainName.org" should redirect to "DomainName.org", where my subdomain name is not fixed. The subdomain will be assigned to each user.
How can I achieve this?

The subdomain is part of the DNS server, and work together with the IIS setup.
So, you can NOT change the DNS setup from asp.net, neither the IIS setup. When a provider gives your the access to add extra sub-domains, what is actually do is that create new entries on the DNS entry, and then add map that to the IIS, so that sub-domains to look at your site. If your provider did not have give you a tool to add sub-domains, nether you can edit the DNS entries, then you can NOT add them from asp.net.
If you can add sub-domains then you can manipulate what you going to server and show on global.asax at Application_BeginRequest using the redirect or the Rewrite the path. For example:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
var SubDomain = GetSubDomain(HttpContext.Current.Request.Url.Host);
if(!String.IsNullOrEmpty(SubDomain) && SubDomain != "www")
{
Response.Redirect("www.yourdomain.com", true);
return;
}
}
// from : http://madskristensen.net/post/Retrieve-the-subdomain-from-a-URL-in-C.aspx
private static string GetSubDomain(Uri url)
{
string host = url.Host;
if (host.Split('.').Length > 1)
{
int index = host.IndexOf(".");
return host.Substring(0, index);
}
return null;
}
Similar posts:
How to remap all the request to a specific domain to subdirectory in ASP.NET
Redirect Web Page Requests to a Default Sub Folder
How to refer to main domain without hard-coding its name?
Retrieve the subdomain from a URL in C#

Related

getting https to work locally in asp.net MVC4 application

I followed this tutorial exactly:
http://www.hanselman.com/blog/WorkingWithSSLAtDevelopmentTimeIsEasierWithIISExpress.aspx
But when I am running locally and try to navigate from a non-https page (like home/index) to and a page I decorated with [RequireHttps] I get the generic "SSL connection error" message.
I hate posting such a generic question, but can you think of anything I have missed? It is a large asp.net mvc4 application, I enabled ssl in the project, it shows the ssl url. Navigating to the ssl url manually does not work either.
HALP!
NOTE: Using IIS Express with visual studio 2012
Per the comment, the error I am getting is Cannot Establish SSL connection.
You shouldn't be using Https when testing locally. I've created my own Https Filter where it will ignore all the local traffic in the localhost and only works either on staging and live environment. You can modify the code to suit you need.
public class RequireSSLAttribute : FilterAttribute, IAuthorizationFilter {
public virtual void OnAuthorization(AuthorizationContext filterContext) {
if(filterContext == null) {
throw new ArgumentNullException("filterContext");
}
if(!filterContext.HttpContext.Request.IsSecureConnection) {
HandleNonHttpsRequest(filterContext);
}
}
protected virtual void HandleNonHttpsRequest(AuthorizationContext filterContext) {
if(filterContext.HttpContext.Request.Url.Host.Contains("localhost")) return;
if(!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {
throw new InvalidOperationException("The requested resource can only be accessed via SSL");
}
string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
//ignore if the request is from a child action
if(!filterContext.IsChildAction) {
filterContext.Result = new RedirectResult(url);
}
}
}
And this is how you use it...
[RequireSSL(Order=1), Authorize(Order=2)]
public PartialViewResult AccountHeader() {
blah...blah...
}
I know it's already answered, but I thought I'd point out the cause of the original error, which the other answers omit.
When you enable SSL on IIS Express, your site is hosted on 2 ports, 1 for http and another for https. When you debug in Visual Studio, the port is usually specified explicitly. The links on your site probably don't specify port numbers, so when you link from a plain http page to a https one, the port number won't change, and you'll request a https page on the plain http port. This is why you get the SSL connection error.
On a real server the port numbers should be implicit, so the problem shouldn't come up, but you'll need to make sure you're using the right port when debugging locally.
You need to add ssl certificate to your site instance in IIS.
To create certifiacte and add it to IIS7 try this tutorial: http://technet.microsoft.com/en-us/library/cc753127(v=ws.10).aspx
After creation you'll be able to add it to your website. Open in IIS 'your website' -> Bindings -> Add and add new host header. Select https, port 443 and select created sertificate.

How to Create Dynamic Sub-domain with ASP.NET?

How can I create a subdomain in an asp.net C# application? I am working with an asp.net portal.
I have my site which redirects all *.domain.com calls to domain.com. What I want to acheive is that first when the user enters a dynamic subdomain name he should be directed to its home page like if user writes www.microsite1.domain.com, then the site should point to page ~/Microsite/Home.aspx?value=microsite1, and when the user accesses www.microsite1.domain.com/about.aspx then i should able to get the argument value1=about.
The first step is to place the subdomain name in the DNS Host Server. To do that you need to manipulate the dns files. For example if you use BIND as DNS server you go and open the text file that keep your DNS configuration eg: "c:\program files\dns\var\mysite.com" and there you add a line as
subdomain.mysite.com. IN A 111.222.333.444
Also you change the ID of the file to give a message to BIND to update the subdomains.
Second step is to redirect the new subdomain to the correct directory. You do that on the protected void Application_BeginRequest(Object sender, EventArgs e) on Global.asax using rewritepath
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.Host.StartsWith("subdomain."))
{
// here you need to find where to redirect him by reading the url
// and find the correct file.
HttpContext.Current.RewritePath("/subdomain/" + Request.Path, false);
}
// .... rest code
}
Its not so easy, not so hard... maybe there are some more minor issues like permissions to write to dns. Also you need to know dns, read the manual about.

How to redirect all .aspx URLs to their NEW location on a subdomain

I recently completed a transfer of a very large ASPDotNetStorefront site (many pages/URLs) from the domain's wwwroot to a subdomain.
For SEO reasons I want to auto 301 redirect any & all OLD URLs which end in ".aspx" to their new location on the subdomain. (All paths being the same except now being in subdomain instead of wwwroot)
Example:
OLD URL= "http://www.mysite.com/c-2-some-product-page.aspx"
DESIRED NEW URL = "http://store.mysite.com/c-2-some-product-page.aspx"
I need something that will check if the requested page ends in .aspx, then redirects to the "store" subdomain. It should also make sure that the requested old page url doesn't exist
This site is running on a Windows host, AppliedI.net
thanks so much
I think you want to try something like this in your global.asax file
// Untested, but the concept is there.
private void Application_BeginRequest(Object source, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().ToLower().StartsWith(
"http://www.mysite.com/"))
{
HttpContext.Current.Response.Status =
"301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location",
Request.Url.ToString().ToLower().Replace(
"http://www.mysite.com/",
"http://store.mysite.com/"));
}
}

URL redirection

When user write http://nextech.pk/ in the browser, I want to redirect to http://www.nextech.pk/ , So I want to embend www to the URL when user write nextech.pk in the browser
How can I accomplish this?
I think I need to write some rules in the web.cofig to accomplish this, but I don't know exactly
Thanks
It will be there as an option in the domain settings page where your domain is registered.
use nextech.pk or www.nextech.pk or both something like that.
Changing there will be the easy one.
just add index.html with:
<meta http-equiv="Refresh" content="0; http://www.example.com">
In the base page for your website or your Default.aspx file, check if the request url is prefixed with www. Otherwise redirect using Response.Redirect to the url prefixed with www.
I don't do this at the ASP.NET level, as it would fail on static content such as images. Simply add an additional website to IIS, with the host header value of all the URLs you wish to redirect FROM (e.g. example.com). On the "Home Directory" tab, click on "A redirection to a URL", and enter the URL you wish to redirect TO.
This allows you to enter http://example.com/foo.txt and end up at http://www.example.com/foo.txt
(NB. These instructions are for IIS 6, they'll vary slightly for IIS 7).
You might want to check with your domain name provider. You should be able to configure your hosting to automatically redirect from nextech.pk to www.nextech.pk.
But you could also do it in Global.asax or with an IHttpModule in the BeginRequest handler:
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication) sender;
if (!application.Request.Url.ToString().Contains("http://www."))
{
application.Response.Redirect(
application.Request.Url.ToString().Replace("http://", "http://www."));
}
}
Both of these methods would work with any request on your website.

Request.ServerVariables["SERVER_NAME"] is always localhost

I'm developing an ASP.NET 3.5 application with Visual Studio 2008.
My default page has some redirection code in the Page_Load method:
protected void Page_Load(object sender, EventArgs e)
{
string sname = Request.ServerVariables["SERVER_NAME"].ToLower();
if (sname.ToLower().Contains("intranet"))
{
Response.Redirect("/intranet/Default.aspx");
}
else if ((sname.ToLower().Contains("extranet")))
{
Response.Redirect("/extranet/Default.aspx");
}
else {
Response.Redirect("/web/Default.aspx");
}
}
I've modified my hosts file so that intranet and extranet redirect to my local machine.
127.0.0.1 intranet
127.0.0.1 extranet
I then type the URL http://extranet in my browser.
However, the problem is that the server variable value returned from Request.ServerVariables["SERVER_NAME"] is always "localhost" and not "extranet"
Any help on how to get the right value?
Many thanks
Request.ServerVariables["HTTP_HOST"] gets the value I was looking for :)
Youre right
You want to retrieve the full address of the website that the request came to. Do not use "SERVER_NAME", use "HTTP_HOST".
Read here,
http://www.requestservervariables.com/get-address-for-website
Server_Name returns the server's host name, DNS alias, or IP address as it would appear in self-referencing URLs
Why don't you use Request.URL?
Your host files only redirect the requests to a specific IP address - you cannot change the requesting machines name by editing them.

Resources