好的,我应该知道这一点......但是......我伸手去拿,因为我把头撞在了墙上。
有一个MVC区域,我们称之为" store"。有一个控制器,我们称它为“家”"。有一个动作,我们称之为" index"。以下网址显示同一页:
/存储/家/索引
/存储/家/
出于搜索引擎优化的目的,我想限制它" / store / home /"。如何做到这一点?
答案 0 :(得分:0)
如果您已使用此URL发布了您的网站,则正确的方法是使用301重定向。这是确保/store/home/index
路线的任何链接不会立即成为死链接的唯一方法,因此不再计入您的搜索引擎优化得分。这可以使用URL rewrite module of IIS完成。
或者,您只需向引用/store/home/
网址的网页添加canonical tag即可。
但是,如果您尚未发布该网站,则可以添加直接转到自定义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>