Using an apache webserver, im currently redirecting hotlinks to a certain image.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?camosreptiles.com.au/ [NC]
RewriteCond %{HTTP_REFERER} !google\. [NC] 
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://i14.photobucket.com/albums/a345/Instar/nedry2.gif [NC,R,L]
</ifModule>

What id like to do, is redirect to a certain page (my own domain) if somebody pastes my image url into a browser, or uses an image url from my site in href. If they use image tags, i want to replace the image.
Not sure if this can be done anymore, I used to do this...

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?camosreptiles.com.au(/)?.*$     [NC]
RewriteRule .*\.(gif|jpg|png|jpeg|bmp)$  [R,NC]

and it used to work, but not anymore. If someone used image tags, it just showed a broken image.

Ive ream books worth of .htaccess stuff, but it all seems to be out of date and not work.

Is the above possible and can you show me how please? P.s. not sure if the coding is right for my . domain?

Dani AI

Generated

Short answer: yes — but you need to tell the server whether the request is for an inline image or for a page load. Redirecting every image request to an HTML page will make embedded <img> tags show the broken-image icon (that explains what saw). Combine a referer check with a test of the Accept header to branch: if the client accepts images, internally rewrite to a replacement image; if it does not (likely a link click or direct navigation), send a redirect to your info page. is correct that blank Referer values are legitimate (proxies, privacy settings), so treat them carefully. ’s idea of a script handler is still useful when you want logging, MIME detection, or finer rules.

Example (mod_rewrite): check Accept, internally serve a local replacement for image requests, otherwise redirect to an info page.

RewriteEngine On

# only touch image requests
RewriteCond %{REQUEST_URI} \.(jpe?g|png|gif|bmp)$ [NC]

# not requested from your own host
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC]

# client accepts images -> serve replacement without redirect
RewriteCond %{HTTP_ACCEPT} image/ [NC]
RewriteRule \.(jpe?g|png|gif|bmp)$ /images/nohotlink.jpg [L]

# otherwise (likely a click/direct) -> external redirect to info page
RewriteCond %{REQUEST_URI} \.(jpe?g|png|gif|bmp)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC]
RewriteCond %{HTTP_ACCEPT} !image/ [NC]
RewriteRule \.(jpe?g|png|gif|bmp)$ /hotlink-info.html [R=302,L]

If you prefer a script (more control, MIME detection, logging), have PHP inspect $_SERVER['HTTP_REFERER'] and $_SERVER['HTTP_ACCEPT'], serve replacement bytes with the correct Content-Type for embedded requests, and do a Location redirect for non-image requests. Detect MIME with finfo or getimagesize and set proper cache/Vary headers.

Caveats: Accept and Referer can be spoofed or absent; test with curl or real browsers. Prefer internal rewrites for embedded images (no R flag) to avoid broken images. For further reading, see the Apache mod_rewrite docs and MDN pages for Accept and Referer headers, and the PHP header/readfile functions for script-based serving.

Recommended Answers

All 6 Replies

Actually I just realised if I block empty referrers, then anyone using a proxy or behind a firewall etc may not be able to see pics on my site.

Is a conditional rewrite rule possible? for example:

If they do this..(anywhere but my domain)

<img src=""/>

They get a specific image instead, BUT..

If they do this....

<a href=""/><a great pic</a>

then when the link is clicked, it redirects to a specific page.

Thats what Id like to do, possible?

No, that does not seem possible. But I might not be understanding your question well enough.

To handle hotlinking:

The way I have seen this done and actually done myself a few times is to have a php script handle all image requests. You have php check for the referrer and output a different image if they do not meet your criteria.

To my knowledge another problem with that code is that the HTTP_REFERER does not always populate with a value. Sometimes it equals a blank value if the user has disabled sending that information in their browser. So it is best not to use HTTP_REFERER as some users will experience difficulties in your website due to having HTTP_REFERER disabled in their browser.

cheers cwarn23, whats an alternate format to HTTP_REFERER ?

There is a simple tutorial at the below link which will explain.
http://corz.org/serv/tricks/htaccess2.php
It is along the lines of the following but you will need to modify the code to suite your needs:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [NC]

Basically what it does is create a symbolic link for the matching path where the path matches the regex and if it matches the regex then it will load the file in the second parameter.

Thanks again. This still doesnt quite allow for 2 options though, two rules depending on how an image is hotlinked, e.g image tag or hef
but ill have a play with it, cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.