Jump to content

Rewrite rule exception for images


127.0.0.1

Recommended Posts

Hi,

 

First some background information. My directory hierarchy where images are stored looks like this...

 

img/

img/icons/

img/photos/

img/banners/

 

I employ a typical rewrite rule to stop outside sites from hotlinking to my images like this and it is placed in the root of my public_html folder...

 

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mywebsite.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://www.mywebsite.com/_errors/hotlinking.png [NC,R,L]

 

However, I noticed that when using some proxies to access my site, the user is fed the hotlinking.png. To combat this I want to allow images only in the root of the img directory to be accessed by outside referers, because images in the root img directory are essential to the sites layout. Though images in its sub-directories such as img/icons/, img/photos/, et cetera should still not be allowed access to outside referers.

 

Rather than creating a RewriteRule for each specific directory I was wondering if it was possible to create one RewriteRule placed in the root of the img/ directory that tells mod rewrite to rewrite all images EXCEPT those in the root img/ directory. In other words, to rewrite images in sub-directories of img ONLY, without explicitly having to list each one in the regular expression.

 

I don't know how to do this correctly with regular expressions but I can illustrate what I want using wildcards to clarify what I'd like...

 

*/img/*/.gif|png|jpg|jpeg = bad

*/img/.gif|png|jpeg|jpeg = ok

Link to comment
https://forums.phpfreaks.com/topic/36722-rewrite-rule-exception-for-images/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.