Forum Moderators: phranque
I'm using mod_rewrite to protect the images on my website, specifically the following code:
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://mydomain.com/.*$ "NC"
RewriteCond %{HTTP_REFERER}!^http://www.mydomain.com/.*$ "NC"
RewriteRule .*\.(gif¦GIF¦jpg¦JPG)$ - "F"
This works fine, except for the web users I have on my site.
E.g. a request for [mydomain.com...] works..
I tried putting a different htaccess file in the webuser directory, using
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://mydomain.com/~webuser/.*$ "NC"
RewriteCond %{HTTP_REFERER}!^http://www.mydomain.com/~webuser/.*$ "NC"
RewriteRule .*\.(gif¦GIF¦jpg¦JPG)$ - "F"
But this doesn't seem to work. Any idea what I'm doing wrong? (and yes, I am actually linking the images in an off-site document, not just requesting them directly in the browser..)
The syntax in your code looks a little strange - specifically, the quotes around the flags instead of square brackets - and you can make it more efficient with the changes below, but if your version works, it works.
RewriteEngine on
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteRule \.(gif¦jpg)$ - [NC,F]
Jim
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mydomain.com/~folder/.*$ [NC]
RewriteRule \.(gif¦jpg)$ - [F]
And that appears to work .. Also get a 403 Forbidden message when I try to load the file in the browser, but that's assumedly a pleasant side-effect :)
Thanks for your help.
If you cut and past the code, be aware that you'll have to change the "¦" broken pipe character back to to a solid pipe before use.
Based on your original code, you might want to add the [NC] (no case) flag to the RewriteRule to restore the original code's case-insensitivity on image filenames. Also, using ".*$" is redundant -- just leave off the end anchor as shown.
RewriteEngine on
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com/~folder/ [NC]
RewriteRule \.(gif¦jpg)$ - [NC,F]
Jim