【问题标题】:How to 301 redirect in ASP.NET 4.0?如何在 ASP.NET 4.0 中进行 301 重定向?
【发布时间】:2012-05-27 05:57:33
【问题描述】:

我正在尝试为网站实现 URL 重定向,而不是逐页进行。我想在 global.asax 文件中执行此操作。下面是我定义的代码。

我希望将http://website.net 作为我的主要网址,并且如果有人输入http://www.website.net,我希望有一个永久的网址重定向。

不幸的是,它不适用于实时网站。谁能指出代码中的问题。代码不会产生任何错误。

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup

    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
    {
        HttpContext.Current.Response.Status = "301 Moved Permanently";
        HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
    }

}

【问题讨论】:

    标签: asp.net redirect search-engine http-status-code-301


    【解决方案1】:

    主要问题:您在Application_Start 中执行上述操作 - 仅执行一次。您应该与每个请求挂钩。试试这个:

    void Application_BeginRequest(object sender, EventArgs e) 
    {
        // Code that runs on every request
    
        if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
        {
            HttpContext.Current.Response.Status = "301 Moved Permanently";
            HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
        }
    
    }
    

    更好的方法是使用 URL 重写,可以在 Web.Config 中进行配置:

    Microsoft rewriting module - Force www on url Or remove www from url

    【讨论】:

    • 糟糕,我的错误。应该注意到...我计划实施 url 路由而不是重写,并且由于结构和不重写,我遇到了重写问题。某些页面中的查询字符串。感谢您的回复。谢谢
    • 最好使用 StartsWith 而不是像这样的 Conatins:if (HttpContext.Current.Request.Url.ToString().ToLower().StartsWith("website.net"))
    • @keivankashani 不,这不是更好。一些网址以 http 或 https 开头,您的方法在这种情况下不起作用。
    【解决方案2】:

    .NET 第 4 版实际上改进了单页实现功能 - redirectpermanent

    Response.RedirectPermanent(NEW_URL);

    【讨论】:

      【解决方案3】:

      如果使用 IIS 7 或更高版本,最简单的解决方案是在 web.config 中使用 httpRedirect 元素。

      <httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent">
           <add wildcard="/MyOldAspFile.aspx" destination="/MyNewFile.aspx" />
           <add wildcard="/MyOldHtmlFile.html" destination="/MyNewFile.aspx" />
      </httpRedirect>
      

      这个方法很强大,比如你换了域名但是页面还是一样的,你只需要添加:

      <system.webServer> 
          <httpRedirect enabled="true" childOnly="true" destination="http://www.mynewdomain.com/" /> 
      </system.webServer>
      

      我在这里写了一篇小文章:ASP.NET 301 permanent redirects: the best solution

      【讨论】:

        【解决方案4】:

        在之前的正确和有用的答案的基础上,这里有几个具体的例子。假设您想删除旧页面(就像我一样),有几个选项。

        选项 1:修改 Global.asax

         void Application_BeginRequest(object sender, EventArgs e)
            {
                // Add permanent redirection for retired pages
                if (Request.Url.LocalPath.ToLower().StartsWith("/[OLD PAGE NAME]"))
                {
                    Response.RedirectPermanent("/[NEW PAGE NAME]", false);
                }
            }
        

        选项 2:修改 web.config

        <system.webServer>
            <httpRedirect enabled="true" httpResponseStatus="Permanent">
                <add wildcard="/[OLD PAGE NAME]" destination="/[NEW PAGE NAME]" />
            </httpRedirect>
        </system.webServer>    
        

        【讨论】:

          【解决方案5】:

          如果您不知道什么是应用程序域名,请使用类似这样的名称

          protected void Application_BeginRequest(object sender, EventArgs e)
              {
                  if(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority).Contains("localhost"))return;
                  var leftPartOfUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority).ToLower();
                  if (leftPartOfUrl.StartsWith("http") && leftPartOfUrl.Split('.').Length == 1)
                  {
                      var fullUrl = HttpContext.Current.Request.Url.ToString();
                      HttpContext.Current.Response.Status = "301 Moved Permanently";
                      HttpContext.Current.Response.StatusCode = 301;
                      HttpContext.Current.Response.AddHeader("Location", fullUrl.Insert(fullUrl.IndexOf("://", StringComparison.Ordinal) + 3, "www."));
                      HttpContext.Current.Response.End();
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-12-21
            • 2019-06-09
            • 1970-01-01
            • 2014-07-31
            • 1970-01-01
            • 2013-11-26
            • 2020-01-24
            • 2018-07-30
            相关资源
            最近更新 更多