14

我正在尝试为网站实现 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"));
    }

}
4

5 回答 5

17

主要问题:您正在执行上述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 重写模块 - 在 url 上强制 www 或从 url 中删除 www

于 2012-05-20T12:14:22.263 回答
13

如果使用 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 永久重定向:最佳解决方案

于 2012-12-11T20:32:01.163 回答
8

.NET 版本 4 实际上对单页实现有一个改进的功能——redirectpermanent

Response.RedirectPermanent(NEW_URL);

于 2012-05-30T06:13:54.103 回答
5

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

选项 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>    
于 2014-01-08T17:00:15.323 回答
2

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

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();
        }
    }
于 2018-01-23T09:01:43.757 回答