301将原始URL请求重定向到路由URL [英] 301 Redirect original URL request to routed URL
问题描述
我正在Global.asax
文件中使用以下代码进行URL重写:
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" }
);
这意味着以下网址:
/Blog/Article/blog-article-title
/Blog/Article/blog-article-title
将加载以下操作:
/Blog/Article?filename = blog-article-title
/Blog/Article?filename=blog-article-title
我注意到原始的URL路径仍将加载我的页面.
I've noticed that the original URL path will still load my page.
如果在网站上线几个月后将URL重写添加到网站,则可能会导致问题. Google将已经对原始URL进行了爬网,现在,当它在网站上对新URL进行爬网时,它将被分类为
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.
我想像原来的URL现在会自动重定向到重写的URL,但是不会.
I imagined that the original URL would now automatically do a redirect to the re-written URL, but it doesn't.
我认为将这样的东西内置到ASP.NET MVC的核心中是有道理的,因为我没有看到仍然可以获取原始URL且没有重定向到重写URL的优点.有什么理由没有这样做吗?
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?
此外,如何防止原始URL加载内容?有没有办法让我 301永久重定向到重写的URL?>
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?
推荐答案
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}
);
这篇关于301将原始URL请求重定向到路由URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!