301 Redirect original URL request to routed URL

一个人想着一个人 提交于 2019-12-08 04:30:42

问题


I'm using the following code in my Global.asax file for url-rewriting:

routes.MapRoute(
               "BlogArticle",
               "Blog/Article/{filename}",
               new { controller = "Blog", action = "Article" }
               );

This means the following URL:

/Blog/Article/blog-article-title

Will load the following Action:

/Blog/Article?filename=blog-article-title

I've noticed that the original URL path will still load my page.

This could cause problems if the URL rewriting was added to the site months after the site went live. Google will have already crawled the original URLs, and when it now crawls the new URLs on the site, it will class this as duplicate content.

I imagined that the original URL would now automatically do a redirect to the re-written URL, but it doesn't.

I think it would make sense for something like this to be built into the core of ASP.NET MVC as I don't see an advantage of still having the original URL obtainable, and not redirected to the re-written URL. Is there any reason this wasn't done?

Also, how can I prevent the original URL from loading the content? Is there a way I can get it to 301 permanent redirect to the re-written URL?


回答1:


routes.MapRoute(
               "BlogArticle",
               "Blog/Article/{filename}",
               new { controller = "Blog", action = "Article" }
               );

try this for cleaner url and then request filename as a parameter in your action

routes.MapRoute(
           "BlogArticle",
           "Blog/Article/{filename}",
           new { controller = "Blog", action = "Article", filename = UrlParameter.Optional}
           );


来源:https://stackoverflow.com/questions/9974402/301-redirect-original-url-request-to-routed-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!