.htaccess - How to allow access to files for all sites while maintaining htaccess redirection
问题描述
I wanted to keep the redirect rule. but I don't want to have to add site by site so that they can display the image. how can i change this rule so that it can do this trick? thanks in advance. Any help is welcome :)
actual htaccess
<Files ~ "\.(jpg|jpeg|png|gif)$">
Order allow,deny
allow from all
</Files>
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)fbclid=
RewriteRule ^ / [L,R=permanent]
RewriteCond %{HTTP_REFERER} !^https://mysite,com [NC]
RewriteCond %{HTTP_REFERER} !^https://www.facebook,com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.facebook,com/ [NC]
RewriteCond %{HTTP_REFERER} !^https://externalsite,com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://externalsite2,com/ [NC]
RewriteCond %{HTTP_REFERER} !^https://mysite,com.*$ [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ https://mysite,com [NC,R,L]
<IfModule mod_php4.c>
php_value engine off
</IfModule>
<IfModule mod_php5.c>
php_value engine off
</IfModule>
<Files ~ "\.((php[0-9]?)|p?html?|pl|sh|java|cpp|c|h|js|rc)$">
Order allow,deny
Deny from all
</Files>
解决方案
RewriteCond %{HTTP_REFERER} !^https://mysite,com [NC] RewriteCond %{HTTP_REFERER} !^https://www.facebook,com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^https://www.facebook,com/ [NC] RewriteCond %{HTTP_REFERER} !^https://externalsite,com/ [NC] RewriteCond %{HTTP_REFERER} !^http://externalsite2,com/ [NC] RewriteCond %{HTTP_REFERER} !^https://mysite,com.*$ [NC] RewriteRule \.(jpg|jpeg|png|gif)$ https://mysite,com [NC,R,L]
I wanted to give full access to other sites to incorporate my content ... redirecting if you access the file directly at
example.com/uploads/test.gif
If you simply want to block direct access (in which case the Referer
header is empty) and allow all other sites to link to your images (basically the opposite of "hotlink protection") then you can replace the above rule (that redirects such requests to the root) with the following:
RewriteCond %{HTTP_REFERER} ^$
RewriteRule \.(jpg|jpeg|png|gif)$ https://example.com/ [NC,R,L]
This redirects all direct requests to the root / home page.
(You had erroneous commas ,
in your original directives that would have prevented this from working?!)
HOWEVER, basing this redirect on the HTTP Referer
is unreliable - you will get false positives. The Referer
is sent by the browser - so the user can control (and suppress) what is sent. The website that is linking to you can also set a referrer-policy that suppresses the HTTP Referer
being sent - so all requests from some sites might look like direct requests anyway and end up being blocked. There is no way around this.
推荐阅读
- javascript - 删除行后提交动态表单问题
- c# - 如何在 xamarin.forms 条目中存储占位符值
- python - Series.index 和 Series.key() 之间的主要区别是什么?他们返回完全相同的结果
- elasticsearch - 如何在普罗米修斯中可视化弹性搜索指标?,两者都安装在 gke 集群中
- java - 如何仅验证类级注释?
- mysql - 获取 SQL 表的另一列中的行对应记录
- java - 无法将 spring-security 切入点从 SpringBoot 1x 迁移到 2x
- slack - 如何从 Slack 获取用户生日日期以进行集成
- r - 有没有办法在不使用“<<-”运算符的情况下将计数器 for 循环重写为 lapply/sapply/map 函数?
- elixir - 如何区分 Elixir 的“with”宏中的错误?