最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Rails Route Redirects for Old Site Links - Stack Overflow

programmeradmin3浏览0评论

I'm trying to map the old links from a previous Coldfusion website to the new Rails website. Previously I could do this in the routes.rb file and it would work.

get "/example-products/index.cfm?cid=3", to: redirect(path: "/products")

If I do this, it works.

get "/example-products", to: redirect(path: "/products")

I'm guessing it doesn't like the .cfm? Can someone fill me in on why this isn't working?

I'm trying to map the old links from a previous Coldfusion website to the new Rails website. Previously I could do this in the routes.rb file and it would work.

get "/example-products/index.cfm?cid=3", to: redirect(path: "/products")

If I do this, it works.

get "/example-products", to: redirect(path: "/products")

I'm guessing it doesn't like the .cfm? Can someone fill me in on why this isn't working?

Share Improve this question asked Feb 14 at 19:13 Tony S.Tony S. 1,2732 gold badges10 silver badges13 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 2

?cid=3 isn't matching properly, if that is a required part of the route you'll have to use :constraints option to match it:

get "/example-products/index",
  constraints: {format: "cfm", params: {"cid" => "3"}},
  to: redirect(path: "/products")

For anything more complex you could create a class that has access to a request object:

class ProductRedirect
  def initialize(cid:)
    @cid = cid.to_s
  end
  def matches?(request)
    request.params["format"] == "cfm" && request.params["cid"] == @cid
  end
end

get "/example-products/index",
  constraints: ProductRedirect.new(cid: 3),
  to: redirect(path: "/products")

https://api.rubyonrails./classes/ActionDispatch/Routing/Mapper/Base.html#method-i-match

发布评论

评论列表(0)

  1. 暂无评论