加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – IIS 6如何从http://example.com/*重定向到http://www

发布时间:2020-12-16 00:02:18 所属栏目:asp.Net 来源:网络整理
导读:我使用的是asp.net 3.5和IIS 6. 我们如何自动将页面从http(s)://example.com/*重定向到http(s)://www.example.com/*? 谢谢. 解决方法 我用HttpModule做了这个: namespace MySite.Classes{ public class SEOModule : IHttpModule { // As this is defined
我使用的是asp.net 3.5和IIS 6.

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

谢谢.

解决方法

我用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:

找到< httpModules>部分在< system.web>中部分,它可能已经有几个其他条目,并添加如下:

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

你可以在这里看到这个:

> http://doodle.co.uk
> http://doodlegraphics.co.uk
> http://www.doodle-graphics.co.uk

一切都在http://www.doodle.co.uk结束

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读