微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我使用的是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结束

原文地址:https://www.jb51.cc/aspnet/251308.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐