使用htaccess拒绝扩展



如果有人试图直接访问特定的扩展名或试图将我的文件包含在第三方网站/跨网站中,我想给它403错误。

示例:

比方说,example.com是我的网站,它有一个图像文件happy.png,所以我希望如果有人访问example.com/happy.pg,它应该显示403错误,而且它不应该在任何其他域中使用,比如<img src="http://example.com/happy.png" />,除了我的域example.com。只有我的域应该使用我的资产。

我想拒绝txt,png,jpg,jpeg,ico,css,js等

.htaccess代码:

<FilesMatch ".txt", "png", "css", "js">
Order Allow,Deny
Deny from All
</FilesMatch>

<FilesMatch ".(htaccess|htpasswd|ini|psd|log|sh)$">
Order Allow,Deny
Deny from all
</FilesMatch>

我的代码怎么了?为什么它不能像我想要的那样工作?

使用此

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com$ [NC]
RewriteRule .*.(txt|ico|jpg|jpeg|gif|png|bmp|js|css)$ - [F,NC,L]

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com [NC]
RewriteRule .(gif|jpeg|js|css)$ - [F,NC,L]

最新更新