sccr410
sccr410

Reputation: 151

htaccess prevent hotlinking image specific file name structure

I have the following in an .htaccess file and it works fine:

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.com [NC]
RewriteRule \.(jpg)$ /noimageforyou [NC,R,L]

This prevents hotlinking of all images. However, I am actually OK with hot linking some images. I am using WordPress, and when you upload an image it will create various image sizes, example:

I am happy to allow hot linking of the smaller image sizes, just not the original. I am wondering if there is a way to refine the htaccess to exclude all as it does now but then allow any filename with #x#.jpg in it.

Upvotes: 0

Views: 165

Answers (1)

Mike Rockétt
Mike Rockétt

Reputation: 9007

You need to add a condition to filter out the smaller images:

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.com [NC]
RewriteCond %{REQUEST_FILENAME} !-\d+x\d+\.jpg # Check the end of the filename only - we don't need the beginning part for this check
RewriteRule \.(jpg)$ /noimageforyou [NC,R,L]

Upvotes: 1

Related Questions