由于 url 编码,url 重写失败 [英] URL rewriting fails because of url encoding
问题描述
我最近在我的 API 项目中实现了版本控制.现在,我的 api 的 URL 必须以序列 /v1/
为前缀(因为我目前在 v1 上)
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)
我不想中断旧的调用,所以为了确保透明兼容,我想重写旧的url.
I don't want to break old calls, so to ensure transparent compatibility, I want to rewrite the old urls.
像 /events/9999/attendees
这样的 URL 应该重定向到 /v1/events/9999/attendees
Url like /events/9999/attendees
should be redirected to /v1/events/9999/attendees
我用我的 rails 路由配置文件 routes.rb
进行了设置,代码如下:
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 }
我现在面临的问题是,最终重写的路由似乎是 url 编码的......(或其他)实际上,这就是 %{path}
好像坏了.然后:
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
被重定向到 /v1/events%2F9999%2Fattendees
我做错了什么?
非常感谢
推荐答案
你可以传递一个块给 redirect
来指定正确的 url
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 }
这篇关于由于 url 编码,url 重写失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!