Search code examples
rubyruby-on-rails-3hotlinking

how to block hotlinking for my media files?


I have swf& images files that can be hotlinked from otherwebsites like in this question How can I use HTTP Referer header to have my swf files viewed only from my website ?

EDIT

My files are hosted at my server, I am not using Amazon S3 service, or other files hosting services.


Solution

  • if request.env['HTTP_REFERER'] =~ /yourdomain\.com\//
      # serve asset
    end
    

    Note that nil =~ regex # => nil so you don't need to check if request.env['HTTP_REFERER'] exists first.

    Edit:

    More about the =~ operator: Object#=~, String#=~, Regex#=~

    $ irb
    > nil =~ /something/
    => nil