使用.htaccess为RoR应用程序提供静态内容

时间:2009-09-23 09:56:21

标签: ruby-on-rails apache .htaccess file-upload shared-hosting

如何让用户能够将其内容上传到我的RoR应用程序,然后再访问以下静态内容:

  1. 由APACHE网络服务器提供[以避免通过Rails的开销],但

  2. 仍希望在访问内容之前进行身份验证/授权检查

  3. 约束是我在DreamHost共享平台上,我只能访问Apache .htaccess文件,我无法添加自己的Apache模块。

1 个答案:

答案 0 :(得分:1)

您可以重定向到静态内容,例如

class ImagesController
  def show
    @image = Image.find(params[:id])
    if user_has_access_to @image
      redirect_to @image.bizarre_and_secret_image_location_that_is_served_by_apache
    else
      access_denied
    end
  end
end

它确实无法完全保护内容。也许暂时使静态URL有用:

RewriteRule ^/images/RANDOMIZED_PREFIX_HERE/(.+)$ images/SECRET_IMAGE_LOCATION/$1 [L]

...现在每小时更改一次.htaccess文件。当然,应用程序也应该知道前缀。