王新阳

wangxinyang

使用.htaccess/web.config文件实现图片防盗链

.htaccess

<Files ~ "^.(htaccess|htpasswd)$">
deny from all
</Files>
order deny,allow
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://.+\.baidu\.com/.+$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(.+\.)*我的域名\.com/.*$ [NC]
RewriteRule .*\.(gif|jpg|jpeg|bmp|png)$ http://imgcache.qq.com/qzone/client/photo/swf/no.gif [R,NC,L]

web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="图片防盗链" stopProcessing="true">
                    <match url=".*\.(gif|jpg|jpeg|bmp|png)$" ignoreCase="true" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{HTTP_REFERER}" pattern="^$" ignoreCase="false" negate="true" />
                        <add input="{HTTP_REFERER}" pattern="^https://.+\.baidu\.com/.+$" negate="true" />
                        <add input="{HTTP_REFERER}" pattern="^https?://(.+\.)?我的域名\.com/.*$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://imgcache.qq.com/qzone/client/photo/swf/no.gif" redirectType="Found" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
2018-01-04
2024-11-22 星期五 农历十月二十二