显示所请求页面的404页面

时间:2020-03-06 14:53:33  来源:igfitidea点击:

我最近将网站迁移到了新的CMS(Umbraco)。许多链接已更改,但是可以通过在URL中搜索模式来轻松纠正它们,因此,我想写一些东西,如果找不到旧的页面,将重定向到正确的页面。那部分不是问题。

将浏览器重定向到我的自定义404页面后,如何获取请求的URL。我尝试使用:

request.ServerVariables("HTTP_REFERER") 'sorry i corrected the typo from system to server.

但这是行不通的。

有任何想法吗?

该站点位于IIS 6.0上。
我们确实考虑过使用301重定向,但是我们无法知道人们为哪些页面添加了书签,并且有几百个页面,因此没有人热衷于花时间创建301重定向。

解决方案

更新,我们实际上想要接听:

VB.NET:

Request.QueryString("aspxerrorpath")

C#:

Request.QueryString["aspxerrorpath"];

怎么样:

Request.ServerVariables("HTTP_REFERER");

A lot of the links have changed, but they can be easily corrected by searching for patters in the url

我们是否考虑过使用URL重写,而不是将用户发送到404,所以?这样,用户(和搜索引擎,如果这对我们而言很重要)将获得301或者302,而不需要通过404处理程序。通常,在服务器上以url级别处理重写要比启动代码并在那里进行处理更快,更省力。

微软已经发布了用于IIS 7的URL重写模块,在这里和这里都对它进行了不错的介绍。

对于IIS 6,这里有一个很好的介绍,介绍如何使用它重写URL,这与IIS7略有不同。

一个重写规则的例子是

#  will contain the contents of (.*) - everything after new-dir/
RewriteRule /new-dir/(.*) /find_old_page.asp?code=
there are a few hundred pages, so no one is keen on spending the time to create the 301's

重写规则的优点在于,我们无需列出即可显式列出所有页面,而可以编写遵循相同模式的规则。我们最近不得不做与此类似的事情,令人惊讶的是,可以通过几个简单的规则来处理多少个移动的url。

我认为不应该使用404页面,而应该使用301 Move Permanently代码重定向。

我做的基本上与我们在自定义404错误处理页面中要求的相同。在IIS 6上,原始URL在查询字符串中。下面的代码显示了如何获取原始URL,然后转发用户。在我的情况下,我从旧的ASP切换到新的ASP.NET,因此所有的.asp页都必须转发到.aspx页。另外,某些网址已更改,因此我会在旧网址中查找关键字并转发。

//did the error go to a .ASP page?  If so, append x (for .aspx) and 
//issue a 301 permanently moved
//when we get an error, the querystring will be "404;<complete original URL>"
string targetPage = Request.RawUrl.Substring(Request.FilePath.Length);

if((null == targetPage) || (targetPage.Length == 0))
    targetPage = "[home page]";
else
{
     //find the original URL
    if(targetPage[0] == '?')
    {
        if(-1 != targetPage.IndexOf("?aspxerrorpath="))
             targetPage = targetPage.Substring(15); // ?aspxerrorpath=
        else
             targetPage = targetPage.Substring(5); // ?404;
        }
        else
        {
             if(-1 != targetPage.IndexOf("errorpath="))
             targetPage = targetPage.Substring(14); // aspxerrorpath=
             else
            targetPage = targetPage.Substring(4); // 404;
        }
    }               

    string upperTarget = targetPage.ToUpper();
    if((-1 == upperTarget.IndexOf(".ASPX")) && (-1 != upperTarget.IndexOf(".ASP")))
    {
        //this is a request for an .ASP page - permanently redirect to .aspx
        targetPage = upperTarget.Replace(".ASP", ".ASPX");
        //issue 301 redirect
        Response.Status = "301 Moved Permanently"; 
        Response.AddHeader("Location",targetPage);
        Response.End();
    }

    if(-1 != upperTarget.IndexOf("ORDER"))
    {
                //going to old order page -- forward to new page
               Response.Redirect(WebRoot + "/order.aspx");
           Response.End();
    }

这是我们在404页上的init上执行的操作:

Dim AttemptedUrl As String = Request.QueryString("aspxerrorpath")
If Len(AttemptedUrl) = 0 Then AttemptedUrl = Request.Url.Query
AttemptedUrl = LCase(AttemptedUrl)
CheckForRedirects(AttemptedUrl)

然后,CheckforRedirects具有自定义逻辑,可以将旧的URL与新的URL进行匹配。

我认为如果我们内部有足够的信息可以将旧系统中的大量URL与新系统进行匹配(例如301或者URL重写),则这是首选方法(而不是301或者URL重写)。如果我们有一个将旧ID与新ID或者类似名称匹配的表。

但是,如果有一个一致的模式可用于使用正则表达式将旧的URL映射到新的URL,那么URL重写将是解决之道。

为了基于"重写"建议,Umbraco已经使用UrlRewriting.NET,可以将新的重写添加到

\config\UrlRewriting.config

希望这可以帮助