htaccess阻止热链接图像特定的文件名结构


htaccess prevent hotlinking image specific file name structure

我在.htaccess文件中有以下内容,它运行良好:

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

这样可以防止对所有图像进行热链接。然而,我实际上对热链接一些图像还可以。我正在使用WordPress,当你上传图像时,它会创建各种图像大小,例如:

  • imagefile.jpg(原始)
  • imagefile-150x150.jpg
  • imagefile-300x400.jpg

我很乐意允许较小图像大小的热链接,只是不允许原始图像。我想知道是否有一种方法可以改进htaccess,像现在这样排除所有文件,但允许任何带有#x#.jpg的文件名。

您需要添加一个条件来过滤掉较小的图像:

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]