Rails 重定向 301 编码 [英] Rails Redirect 301 encoding

查看:52
本文介绍了Rails 重定向 301 编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重新创建了一个网站并且有很多 301 需要处理(从 php url 到 Rails url).

I have recreated a website and have lots of 301 to handle (from php urls to Rails urls).

它与:

match "/produits/les-dallages/dallage-riva.html", :to => redirect("/produits/dallages/dalle-riva")

我的问题是针对这种旧网址(由谷歌网站管理员工具提供):

My problem is for this kind of old urls (providing from google webmaster tools):

"/produits/les-pavages/paves-carres/item/48-pav%C3%A9s-carr%C3%A9s.html"

无法理解编码,因为 url 是由浏览器转换的,Rails 无法理解带有é"而不是%C3%A9"的 url...

The encoding is not understood because the url is transformed by the browser and Rails didn't understand the url with "é" instead of "%C3%A9"...

如何管理这种网址?

第二个问题:我可以在routes.rb文件中添加多少条路由(301)?

Second question: how many routes (301) can I add in the routes.rb file ?

谢谢

推荐答案

理论上,您可以添加许多您想要的路线.但是,我们不应该在路由文件中放置不必要的内容,因为它会占用内存,并且需要一些时间处理每个请求的所有路由逻辑才能到达控制器.

In theory, you could add many routes you want. However, we should not put unnecessary in the routes file because it would eat up memory, and it needs to some times to process all the routes logic for every request before it can go to the controller.

如果您有相当多的 url 进行重定向,而不是弄乱路由文件,我建议您创建一个控制器来进行重定向,因为您可以编写更灵活的代码.也许您可以创建一个表来存储 from_url (旧 url)和 new_url (用于重定向).然后,在一个新的控制器中,只需在数据库中找到旧的 url 并进行重定向.

In case you have quite a lot of urls to do redirection, rather than mess up with routes file, I would recommend you create a controller just to do redirection, because you could write a lot more flexible code. Maybe you could create a table to store from_url (old url) and new_url (for redirection). Then, inside a new controller, simply find the old url in the database and do a redirect.

class RedirectionController < ApplicationController
  def index
    redirect = Redirection.find_by_from_url(request.request_uri)
    if redirect
      redirect_to redirect.to_url, :status => :moved_permanently
    else
      render 'public/404', :status => :not_found, :layout => false
    end
  end
end

最后,使用 Route Globbing 匹配任何 URL 以进行重定向.您可以在 http://guides.rubyonrails.org/routing.html 上查看更多信息

Last, use Route Globbing to match any urls for redirection. You can check out more about it on http://guides.rubyonrails.org/routing.html

match '/produits/*' => 'redirection#index'

对于像é"这样的重音字符,您只需将该值存储在您的数据库中.对于 MySQL,您应该将数据库服务器配置为存储 utf-8 并更新 database.yml 中的连接.

For accent characters like 'é', you simply need to store this value inside your database. For MySQL, you should configure your database server to store utf-8 and update the connection inside database.yml.

encoding: utf8
collation: utf8_unicode_ci

您可以尝试通过以下代码重定向.它工作得很好.它必须在文件的开头有 # encoding: UTF-8 因为有那些重音字符.

You can try to redirect by the following code. It works perfectly okay. It must have # encoding: UTF-8 at the beginning of the file because there are those accent characters.

# encoding: UTF-8
class RedirectionController < ApplicationController
  def index
    redirect_to "produits/les-pavages/paves-carres/item/48-pavés-carrés"
  end
end

这篇关于Rails 重定向 301 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆