【问题标题】:subdomain url redirects to the main url子域 url 重定向到主 url
【发布时间】:2012-12-25 13:05:30
【问题描述】:

我在 asp.net 中开发了一个网站并托管在 windows 服务器上,它没有为我提供子域名的通配符条目。我如何将我的网址写为例如 http://subdomainName.DomainName.org?

我想将带有子域的 url 重定向到主域;所以url“subdomainName.DomainName.org”应该重定向到“DomainName.org”,我的子域名不固定。子域将分配给每个用户。

我怎样才能做到这一点?

【问题讨论】:

  • 什么?是否要重定向 *.domain.org -> domain.org?我想 URL 重写模块可以帮助你实现这一点。

标签: asp.net c#-4.0 subdomain


【解决方案1】:

subdomain 是 DNS 服务器的一部分,与 IIS 设置一起工作。

因此,您不能从 asp.net 更改 DNS 设置,也不能更改 IIS 设置。当提供商允许您添加额外的子域时,实际上所做的是在 DNS 条目上创建新条目,然后将映射添加到 IIS,以便子域查看您的站点。如果您的提供商没有给您添加子域的工具,您也可以编辑 DNS 条目,然后您不能从 asp.net 添加它们。

如果您可以添加子域,那么您可以使用redirect 或重写路径来操作您要在Application_BeginRequest 上的服务器和显示在global.asax 上的内容。例如:

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;
}

类似帖子:
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#

【讨论】:

  • 更正:HttpContext.Current.Request.Url.Host
猜你喜欢
  • 2012-02-15
  • 1970-01-01
  • 2014-05-10
  • 2017-02-20
  • 2012-05-25
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
相关资源
最近更新 更多