Ruby on Rails 301 redirection

女生的网名这么多〃 提交于 2019-12-04 21:11:56

问题


I added slugs to some of the models, but because of SEO I need to do 301 redirection from old links: old:

http://host.com/foo/1

new:

http://host.com/foo/foo_slug

question: how to implement 301 redirection in this case? and is it possible to implement 301 redirection from uppercased link? Like this:

http://host.com/foo/FOO_SLUG -> http://host.com/foo/foo_slug

回答1:


You should be able to redirect with status 301 by adding this to your controller action:

redirect_to "http://host.com/foo/foo_slug", :status => 301

See the API documentation for redirect_to for details.

And there should be no problem with redirecting upper-cased URLs to lowercased versions, although this may be better handled by something at the HTTP server layer such as Apache mod_rewrite rules.




回答2:


For 301 redirection write this code in your controller:

headers["Status"] = "301 Moved Permanently"

redirect_to "http://host.com/foo/foo_slug" # in your case

And for second question, use capitalise or down case if you mentioned hardcode url.

Otherwise use ruby interpolation by putting whole url in string



来源:https://stackoverflow.com/questions/15872233/ruby-on-rails-301-redirection

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