如果您已经使用此 URL 发布了您的网站,那么正确的做法是使用 301 重定向。这是确保任何指向 /store/home/index 路由的链接不会立即变成死链接并因此停止计入您的 SEO 分数的唯一方法。这可以使用URL rewrite module of IIS 来完成。
或者,您可以将canonical tag 添加到指向/store/home/ URL 的页面。
但是,如果您还没有发布网站,您可以添加直接进入自定义 404 页面的路由。
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Store_Non_Match",
"store/home/index",
new { controller = "System", action = "Status404"}
).DataTokens["area"] = "";
context.MapRoute(
"Store_default",
"store/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
然后在您的站点中,制作一个系统控制器以返回 404 页面和状态。
public class SystemController : Controller
{
//
// GET: /System/Status301/?url=(some url)
public ActionResult Status301(string url)
{
Response.CacheControl = "no-cache";
Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
Response.RedirectLocation = url;
ViewBag.DestinationUrl = url;
return View();
}
//
// GET: /not-found
public ActionResult Status404()
{
Response.CacheControl = "no-cache";
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
}
}
请注意,上面的控制器还演示了如何在应用程序中使用 301 重定向来替代 IIS 重写模块。如果您知道您将随着时间的推移而停用的 URL 并且您希望通过应用程序中的操作自动执行此操作,这将非常方便。并非所有浏览器都遵循 301 重定向,因此我的解决方案是返回一个视图,该视图在 5 秒后尝试同时执行 JavaScript 和元刷新重定向,如果所有其他方法均失败,则它具有指向用户可以单击的页面的超链接。
// Status301.cshtml
@{
ViewBag.Title = "Page Moved";
}
@section MetaRefresh {
<meta http-equiv="refresh" content="5;@ViewBag.DestinationUrl" />
}
<h2 class="error">Page Moved</h2>
This page has moved. Click this link if you are not redirected in 5 seconds: <a href="@ViewBag.DestinationUrl">@ViewBag.DestinationUrl</a>.
<script>
//<!--
setTimeout(function () {
window.location = "@ViewBag.DestinationUrl";
}, 5000);
//-->
</script>