【问题标题】:Rails Redirect 301 encodingRails 重定向 301 编码
【发布时间】:2012-05-28 16:06:02
【问题描述】:

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

完美搭配:

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

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

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

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

如何管理此类网址?

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

谢谢

【问题讨论】:

    标签: ruby-on-rails redirect


    【解决方案1】:

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

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

    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查看更多信息

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

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

    encoding: utf8
    collation: utf8_unicode_ci
    

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

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

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2012-03-13
      相关资源
      最近更新 更多