问题原文
原帖链接 · DonP · CC BY-SA 4.0
This set of rules causes the nolinks.jpg image to appear if anyone tries to link directly to an image from a remote site. It is working well but with the latest Firefox, the browser is forced to https rather than http. How can it be made to work whether https, http? I rarely use regular expressions so not sure how to apply them here.
# Block external links
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain1\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain1\.net/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain1\.org/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain2\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain2\.net/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain2\.org/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?devdomain\.dev/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule show_image\.php http://www.domain1.com/nolinks.jpg? [R=301,QSA]
If forced to https, the image would have to link to https too. Also, it needs to work whether www or no www. How is this done?
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
There's nothing in the directives you've posted that would cause this directly. Throwing a regex at this isn't going help. The RewriteRule directive you posted redirects to http - end of. So, any "redirect" to HTTPS is coming from somewhere else.
You need to examine the HTTP request/response headers in the browser. Are you actually seeing a 3xx redirect or is the browser "upgrading" the request? Are you sending a Content-Security-Policy: upgrade-insecure-requests; HTTP response header? Or even Strict-Transport-Security header (although that is only relevant when sent over HTTPS). Have you dabbled with HSTS in the past?!
sometimes, especially with the latest Firefox
However, the fact that this appears to be intermittent and browser dependent would seem to suggest the problem is elsewhere and harder to debug.
Since the "big push" to HTTPS everywhere, it seems strange that any site would be hotlinking an HTTP image - this would cause problems if the source site is HTTPS. Maybe the source site is triggering the upgrade/redirect to HTTPS?!
However, there are some concerns with the directives as posted.
There is no
Lflag on theRewriteRule, so processing is going to continue through the file and potentially be rewritten or redirected by later directives. In other words, there could be a conflict with other directives. However, you've stated that there are "no other rewrite-related directives" - so that would seem to rule out this possibility. Although it is the most likely one.[R=301,L]You've included the
QSAflag on theRewriteRuleand included a trailing?on the substitution string (ie./nolinks.jpg?). This is really a contradiction. The trailing?effectively removes any query string from the original request, but theQSAflag appends it again! You would get the same result without the trailing?and without theQSAflag, however, you do want to remove the query string. So, you should keep the trailing?and remove theQSAflag. Or, use theQSD(Query String Discard) flag instead on Apache 2.4.RewriteRule show_image\.php http://www.domain1.com/nolinks.jpg [QSD,R=301,L]You are using a 301 (permanent) redirect. This reduces the hits to your server as the redirect response should be cached by the browser. However, it also means that if a user should visit a site that is hotlinking your images and they are redirected to
nolinks.jpgthen they will also be redirected when they visit your site and will be unable to see your images. You would need to change this to a 302 (temporary) redirect to avoid this behaviour - but maybe that's considered an edge case and you want to keep this "permanent". However, a "cached redirect" could perhapsYou are redirecting to HTTP and checking HTTP in the
Refererheader in all your conditions. Are you not using HTTPS on any of your sites?!Instead of redirecting to another image, it would be far more efficient and less resource-intensive (part of the reason for creating a hotlink protection script in the first place) if you simply block (403 Forbidden) the request instead. For example:
RewriteRule show_image\.php - [F]The use of
RewriteCond %{HTTP_REFERER} !^$is arguably necessary since some users might suppress the HTTPRefererand without this condition they would not be able to see your images. Although you could argue that the user knows what they are doing in this case and have made the intentioned decision that they may not see some images. However, this condition also allows a site that is hotlinking your images to set a Referrer-Policy that suppresses theRefererand overrides your hotlinking script!
UPDATE:
...caused by HTTPS-only mode which was introduced in FF 83 .... Fortunately the site is accessible by https ... so the only real problem is my own .htaccess making none of the images show up. ... the .htaccess rewrite rules to work whether http or https
Ah, in that case, as touched on in #4 above, you need to modify all instances of http in the RewriteCond directives to https? - the ? quantifier makes the s optional.
For example:
RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?domain1\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?domain1\.net/ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?domain1\.org/ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?domain2\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?domain2\.net/ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?domain2\.org/ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?devdomain\.dev/ [NC]
Whether you change the http (to https) in the RewriteRule substitution string is really up to you (if you are not forcing HTTPS on your site)¹:
RewriteRule show_image\.php https://www.domain1.com/nolinks.jpg? [R=301,L]
(NB: Just https, not https? in the substitution string.)
¹ But note that if you redirect to plain HTTP then any HTTPS site that hotlinks your image(s) - where the user is not using "HTTPS-only mode" in Fx (or similar plugin in other browsers) - will get a "mixed content" warning in their browser and the nolinks.jpg image won't display.
Or, simply block the request (preferable from a server perspective) as mentioned in #5 above:
RewriteRule show_image\.php - [F]
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
