【问题标题】:IIS 6 how to redirect from http://example.com/* to http://www.example.com/*IIS 6 如何从 http://example.com/* 重定向到 http://www.example.com/*
【发布时间】:2009-12-09 07:37:48
【问题描述】:

我正在使用 asp.net 3.5 和 IIS 6。

我们如何自动将页面从http(s)://example.com/* 重定向到http(s)://www.example.com/*

谢谢。

【问题讨论】:

    标签: asp.net iis-6


    【解决方案1】:

    我用 HttpModule 做到了这一点:

    namespace MySite.Classes
    {
      public class SeoModule : IHttpModule
      {
        // As this is defined in DEV and Production, I store the host domain in
        // the web.config: <add key="HostDomain" value="www.example.com" />
        private readonly string m_Domain =
                                WebConfigurationManager.AppSettings["HostDomain"];
    
        #region IHttpModule Members
    
        public void Dispose()
        {
          //clean-up code here.
        }
    
        public void Init(HttpApplication context)
        {
          // We want this fire as every request starts.
          context.BeginRequest += OnBeginRequest;
        }
    
        #endregion
    
        private void OnBeginRequest(object source, EventArgs e)
        {
          var application = (HttpApplication) source;
          HttpContext context = application.Context;
    
          string host = context.Request.Url.Host;
          if (!string.IsNullOrEmpty(m_Domain))
          {
            if (host != m_Domain)
            {
              // This will honour ports, SSL, querystrings, etc
              string newUrl = 
                   context.Request.Url.AbsoluteUri.Replace(host, m_Domain);
    
              // We would prefer a permanent redirect, so need to generate
              // the headers ourselves. Note that ASP.NET 4.0 will introduce
              // Response.PermanentRedirect
              context.Response.StatusCode = 301;
              context.Response.StatusDescription = "Moved Permanently";
              context.Response.RedirectLocation = newUrl;
              context.Response.End();
            }
          }
        }
      }
    }
    

    然后我们需要将模块添加到我们的 Web.Config 中:

    &lt;system.web&gt; 部分中找到&lt;httpModules&gt; 部分,它可能已经有几个其他条目,并添加如下内容:

    <add name="SeoModule" type="MySite.Classes.SeoModule, MySite" />
    

    您可以在此处查看此操作:

    全部都在http://www.doodle.co.uk

    【讨论】:

    • 没问题(为什么说谢谢这么难?哦,因为我要在cmets框中输入一篇文章。我记得这个框只有不到200个字符。现在看,要求很多字符数,总共接受 600 个字符 - 我告诉你,这太臃肿了!)
    • 非常好!对我来说效果不佳的一件事是,如果我有 URL 重写。我发现将 newUrl 字符串更改为以下(在 VB 中)解决了这个问题: Dim newUrl As String = String.Format("http{0}://{1}", IIf(CBool​​(context.Request .ServerVariables("HTTPS") = "off"), "", "s"), m_Domain + context.Request.RawUrl)
    【解决方案2】:

    这个MSDN page 可能会对你有所帮助。

    【讨论】:

      【解决方案3】:

      一般来说,如果让 IIS 处理重定向,性能会更好。为此,请创建一个主机标头设置为 example.com 的新网站,并使用 IIS 管理器配置重定向。

      【讨论】:

      【解决方案4】:

      我认为最好使用 DNS。

      【讨论】:

      • 在不知道发布者为什么要这样做的情况下,DNS 不太可能是一种合适的方法恕我直言。首先,我能想到在 DNS 中这样做的唯一方法是使用 CNAME,这不是一个非常好的方法。其次,考虑到问题的标记方式,发帖人很可能特别想在 IIS 或代码中进行重定向。
      • @Rob - 同意 - 通常,要求是将所有可能的主机名解析为一个,以确保搜索引擎只列出您网站的一个实例 - 因为 OP 询问“我们如何重定向doming.com 到 www.domain.com”,我会假设 DNS 已经配置,并且两个版本都服务于同一个站点。
      猜你喜欢
      • 1970-01-01
      • 2011-05-18
      • 2014-10-02
      • 1970-01-01
      • 2016-01-16
      • 2017-12-21
      • 2020-09-24
      • 2012-10-17
      • 2020-12-21
      相关资源
      最近更新 更多