我正在对我的网站进行SEO优化,目前正在处理网址。我已经删除了最后一个斜杠并从
添加了重定向现在我要从网址中删除所有额外的斜杠: 此网址:
www.exaple /约///毕业
应该重定向到
www.example /左右/毕业。
我在SO中发现了类似的问题,但它们似乎是在纯ASP.NET的背景下提出的。 Using System.Uri to remove redundant slash
Remove additional slashes from URL
如何在MVC5中实现相同功能?
答案 0 :(得分:3)
在Global.asax中使用代码隐藏重定向,如下所示;
protected void Application_BeginRequest(object sender, EventArgs e)
{
string requestUrl = Request.ServerVariables["REQUEST_URI"];
string rewriteUrl = Request.ServerVariables["UNENCODED_URL"];
if (rewriteUrl.Contains("//") && !requestUrl.Contains("//"))
Response.RedirectPermanent(requestUrl);
}
我从This Post获得了此代码,我希望这对您有用=]