简体   繁体   English

Ruby on Rails 301重定向

[英]Ruby on Rails 301 redirection

I added slugs to some of the models, but because of SEO I need to do 301 redirection from old links: old: 我添加了slugs到一些模型,但由于SEO我需要从旧链接做301重定向:旧:

http://host.com/foo/1

new: 新:

http://host.com/foo/foo_slug

question: how to implement 301 redirection in this case? 问题:在这种情况下如何实现301重定向? and is it possible to implement 301 redirection from uppercased link? 是否可以从大写链接实现301重定向? Like this: 像这样:

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

You should be able to redirect with status 301 by adding this to your controller action: 您应该能够通过将其添加到控制器操作来重定向状态301:

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

See the API documentation for redirect_to for details. 有关详细信息,请参阅redirect_to的API文档。

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. 将大写URL重定向到小写版本应该没有问题,尽管HTTP服务器层可能会更好地处理这些问题,例如Apache mod_rewrite规则。

For 301 redirection write this code in your controller: 对于301重定向,请在控制器中编写此代码:

headers["Status"] = "301 Moved Permanently" headers [“Status”] =“301永久移动”

redirect_to " http://host.com/foo/foo_slug " # in your case redirect_to“ http://host.com/foo/foo_slug ”#在你的情况下

And for second question, use capitalise or down case if you mentioned hardcode url. 对于第二个问题,如果您提到硬编码网址,请使用大写或小写。

Otherwise use ruby interpolation by putting whole url in string 否则通过将整个url放在字符串中来使用ruby插值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM