0

I have a folder on my site which contains several mp4 files. I have a php page on my site which is used to play those mp4 videos. I am new to htaccess and what I need is an htaccess that allows my videowindow.php to access the mp4 files but prevent access to those files if not from videowindow.php

Basically unless the video is being accessed by videowindow.php the video can not be accessed. I tried several examples I found here and there but none of them seem to work. Most of them deny access to the video files completely so they can't even be accessed by my own videowindow.php file.

Any help on this is appreciated.

Matthew Wilson
  • 2,565
  • 2
  • 13
  • 5

1 Answers1

0

You can do this by checking the HTTP "Referer" request field, but this in no way guarantees any access restrictions. The referer field can be easily forged.

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://your-domain\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} !videowindow\.php$
RewriteRule \.mp4$ - [L,F]

This will make it so if a referer doesn't start with your domain and doesn't end with videowindow.php, then any access to mp4's will result in a 403 forbidden response.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • What do I do if the videowindow.php file has a variable being passed to it? Like videowindow.php?video=path/to/video/nameofvideo.mp4 – Matthew Wilson Oct 17 '13 at 16:43
  • Alright, my server looks like this. www.mysite.com/mainfolder/videowindow.php?video=path/to/video/video.mp4 – Matthew Wilson Oct 17 '13 at 16:56
  • @MatthewWilson I don't know what you mean by passing a variable to your php file as I don't know what the code does. Does it read the file and send it directly to the brower? Does it create a link that you can click on? Does it do something else? I have zero context here... – Jon Lin Oct 17 '13 at 17:11
  • Well, first off. Even if I change your line !videowindow\.php$ to !index\.php$ it won't even let me view the video on the index.php page. – Matthew Wilson Oct 17 '13 at 17:15
  • @MatthewWilson No idea what the code in index.php does. Does it link directly (as in, it creates a link on the page directly to the mp4 file that I can click on)? Does it read from the filesystem and serve it directly to the browser? – Jon Lin Oct 17 '13 at 17:16
  • index.php is just a page located at www.mydomain.com/sitefolder/index.php and it has a video player on it that directly links to the mp4 video. It is a standard html5 video tag. – Matthew Wilson Oct 17 '13 at 17:19
  • Thanks for the help but I wound up getting lucky on a search and found the answer here, kind of similar to yours but it's working for me and serves my purpose well enough: http://stackoverflow.com/questions/10236717/htaccess-how-to-prevent-a-file-from-direct-url-access – Matthew Wilson Oct 17 '13 at 17:27