[英]Redirecting a path to a new domain in Rails routes
I want to redirect requests for a bunch of files/folders in a directory on one site to a directory (and then identical path) on a new site. 我想将对一个站点上的目录中的一堆文件/文件夹的请求重定向到新站点上的目录(然后是相同的路径)。
In my Rails routes.rb I have: 在我的Rails route.rb中,我有:
match '/uploads/*path', :to => redirect{|params, request| "http://othersite.com/uploads/#{params[:path]}"}
which I've got from this answer . 这是我从这个答案中得到的。
It almost works, except this URL: 除了以下URL,它几乎可以工作:
http://currentsite.com/uploads/some/directories/filename.pdf
redirects to: 重定向到:
http://othersite.com/uploads/some/directories/filename
I can't work out why the file extension is missing. 我无法弄清楚为什么文件扩展名丢失了。
(I would also add something like constraints: {:path => /[\\w\\-_\\/]+(\\.[az]+)?/}
to the end to ensure the path is vaguely valid, but I've left that off while trying to fix this issue.) (我还将在结尾添加类似
constraints: {:path => /[\\w\\-_\\/]+(\\.[az]+)?/}
,以确保该路径模糊有效,但是我尝试解决此问题时,将其忽略了。)
You want to include the request format: 您要包括请求格式:
match '/uploads/*path', :to => redirect{|params, request| "http://othersite.com/uploads/#{params[:path]}#{ '.'+params[:format] if params[:format].present? }"}
... this is the portion of the URI we think of as the file extension (ie: .pdf
in your example). ...这是URI的一部分,我们认为它是文件扩展名(即您的示例中的
.pdf
)。
Though, it may be more elegant to just use the full incoming request path (via request.full_path
, which has the URI path and all query params): 不过,仅使用完整的传入请求路径(通过
request.full_path
,它具有URI路径和所有查询参数)可能会更优雅:
match '/uploads/*path', :to => redirect{|params, request| "http://othersite.com/uploads/#{ request.fullpath }" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.