大网数据广告活动
网站运营与故障问答

Htaccess Hotlinking Prevention: Exclude a specific image from rule

· 52CE 我爱测

I have a working hotlinking prevention in the .htaccess . Everything works fine. However, I use an image of my website in email signature. After the hotlinking prevention, this image is now displayed in old mails also with the pl…

问题原文

原帖链接 · dajana.s · CC BY-SA 4.0

I have a working hotlinking prevention in the .htaccess. Everything works fine. However, I use an image of my website in email signature. After the hotlinking prevention, this image is now displayed in old mails also with the placeholder image (the image that describes that hotlinking is not allowed). Since I can't change the code for old mails from customers etc. of course, I want to exclude the old signature image from the hotlinking prevention. What line of code do I need to add to do this? I have tried various things and somehow it does not work.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mywebsite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?bing.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yahoo.com/.*$ [NC]
RewriteRule \.(jpg|jpeg|png|gif|svg|webp)$ https://www.dropbox.com/s/XXXXXXXXX/stop-hotlinking.png?raw=1 [NC,R,L]

Which line do I have to insert if my old signature image behind the following address and should be excluded from hotlinking prevention? Just this one file only.

https://www.mywebsite.com/wp-content/uploads/old_signature.gif

社区高分解答

原帖链接 · MrWhite · CC BY-SA 4.0

To allow that specific file, excluding it from the hotlink-protection script you just need to add the following condition (RewriteCond directive) to the existing list of conditions:

RewriteCond %{REQUEST_URI} !=/wp-content/uploads/old_signature.gif

This checks that the requested URL-path (ie. the value of the REQUEST_URI server variable) is not (denoted by the ! prefix) the URL of the image you want to allow.

The ! prefix negates the expression, so it is successful when it does not match (as with all the other conditions on this rule).

The = prefix operator makes this a lexicographical string comparison, as opposed to a regex (the default). So, this is essentially an "exact match" and no need for anchors or additional character escaping (as would be required with a regex).

Requests for this image (this exact URL) are then allowed through unencumbered.

The complete code would then look like this:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mywebsite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?bing.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yahoo.com/.*$ [NC]
RewriteCond %{REQUEST_URI} !=/wp-content/uploads/old_signature.gif
RewriteRule \.(jpg|jpeg|png|gif|svg|webp)$ https://www.dropbox.com/s/XXXXXXXXX/stop-hotlinking.png?raw=1 [NC,R,L]

Alternative - separate rule (exception)

Alternatively, if you don't have any other mod_rewrite rules in your .htaccess file that should apply to this request (unlikely) then you could instead add an additional rule (an exception) before the existing hotlink-protection script, at the very top of the .htaccess file.

For example:

# Prevent further processing if this file is requested
RewriteRule ^wp-content/uploads/old_signature\.gif$ - [L]

# Existing hotlink-protection script follows...
:

Note that this "alternative" solution assumes the .htaccess file is located in the document root, not a subdirectory.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2022-08-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。