View Full Version : .htaccess for hotlinking

NW Dave
04-05-2010, 07:59 PM
Hi Folks,

Added this .htaccess file to root to block hotlinking as per here: http://www.javascriptkit.com/howto/htaccess10.shtml

RewriteBase /images
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?pitman.us/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.pitman.us/images/angryman.jpg [R,L]

I added the first line of code as per instructions from server owner. (see below)
I placed the file in the root, and in the folder "images". Doesn't seem to work.
Can someone please point out where I went off the tracks?

Thank you,

My question to host:

I wanted to set an .htaccess file to stop hotlinking. It is dependant on the the server having 'mod rewrite' enabled.
Is it so?

Their Reply:


We use virtual user directory paths, so you can get error when trying to setup search engine friendly URLs or trying to pass virtual directory names to PHP scripts. If can be fixed very easy. Edit your .htaccess file and add this line at the top of the file or before the first rewrite rule:

RewriteBase /

Note: if your script is installed on some directory, for example /forum, you have to place RewriteBase /forum line to the .htaccess file (.htaccess file must be also located in /forum directory)

NW Dave
04-06-2010, 08:58 PM
So, I'm guessing by no responses, that the code is correct, and the problem lies elsewhere?

04-07-2010, 03:46 AM
If your domain is pitman . us

I believe the response you received regarding RewriteBase would be more like
RewriteBase /
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?pitman.us/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.pitman.us/images/angryman.jpg [R,L]

Then if you placed this in the location pitman.us/.htaccess

It would be in the main directory / folder of pitman . us so any folders/subfolders such as /images are covered by this .htaccess. The subfolders / directories obey the top level .htaccess unless you add a custom one in a particular folder to over-ride it.

I can't say for sure about the rest of the code right off hand, just the RewriteBase / part.

NW Dave
04-07-2010, 06:16 PM
Gameutopia, thanks for your reply;

I originally tried the code exactly as you have it, with no success, and simply added the images to Rewrite Base / as an attempt to have it work.

I can still link directly to an image file within the folder /images, so for some reason, the server is not parsing the .htaccess file, or I'm not understanding it's true function.