问题
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