【发布时间】:2026-02-19 20:30:01
【问题描述】:
我有一个名为“课程”的资源类型。由于技术原因(与 adobe Shockwave 插件有关),一些较旧的课程只能在 http 页面上播放,而不是 https。
我试图在课程控制器的“显示”操作中处理这个问题,如下所示:(ldb 是一个日志记录命令)
def show
#we can't play shockwave in https, so redirect to http if we're in https mode and the lesson has a shockwave resource
if @lesson.needs_shockwave? && request.protocol == "https://"
ldb "request.url = #{request.url}"
new_url = request.url.gsub(/^https/,"http")
ldb "redirecting to #{new_url}"
redirect_to new_url and return
else
respond_to do |format|
format.html
format.pdf { render :layout => false }
format.js
end
end
end
我可以在日志中看到它生成了新的 http 版本的 url:
### request.url = https://local.charanga.com/lessons/134726?foo=bar
### redirecting to http://local.charanga.com/lessons/134726?foo=bar
但是,它重定向到的实际 url 在开始时仍然有 https,因此它陷入了不断重定向回自身的循环中。为什么会发生这种情况?
【问题讨论】:
标签: ruby-on-rails redirect https