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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.