URL rewriting fails because of url encoding

戏子无情 提交于 2019-12-11 00:55:06

问题


I recently implemented versioning on my API project. Now URLs of my api will have to be prefixed by the sequence /v1/ (As I'm currently on v1)

I don't want to break old calls, so to ensure transparent compatibility, I want to rewrite the old urls.

Url like /events/9999/attendees should be redirected to /v1/events/9999/attendees

I set this up with my rails routes config file routes.rb with the following code :

match "*path", :to => redirect("/#{API_CONFIG['current_version']}/%{path}"),
:constraints => lambda { |request| true }

The issue I'm facing now is that the final rewritten route seems to be url-encoded ... (or something) Actually, that's the %{path} that seems to be broken. Then :

/events/9999/attendees is redirected to /v1/events%2F9999%2Fattendees

What am I doing wrong ?

Many thanks


回答1:


you can pass a block to redirect to specify the correct url

match "*path", :to => redirect {|params, request| "/v1/#{params[:path]}"}, :constraints => lambda { |request| true }


来源:https://stackoverflow.com/questions/18338816/url-rewriting-fails-because-of-url-encoding

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