我最近将网站迁移到了新的CMS(Umbraco)。许多链接已更改,但是可以通过在URL中搜索模式来轻松纠正它们,因此,我想写一些东西,如果找不到旧的页面,将重定向到正确的页面。那部分不是问题。
将浏览器重定向到我的自定义404页面后,如何获取请求的URL。我尝试使用:
request.ServerVariables("HTTP_REFERER") 'sorry i corrected the typo from system to server.
但这没有用。
有任何想法吗?
该站点位于IIS 6.0上。
我们确实考虑过使用301重定向,但是我们无法知道人们为哪些页面添加了书签并且有几百个页面,因此没有人愿意花时间创建301重定向。
最佳答案
我做的基本上与您在自定义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();
}
关于c# - 显示请求页面的404页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/150329/