.htaccess重定向图像链接

时间:2017-10-01 08:39:45

标签: .htaccess redirect

我想知道是否可以在.htaccess文件中设置重定向,以便在其他网站直接链接到我网站上的图片时,而不是在其上的浏览器窗口中打开图像拥有,托管图像的页面显示。

此页面与图像同名,即如果其他网站链接的图像是

案例1

www.example.com/subdirABC/gallery/image.jpg
**to**
www.example.com/subdirABC/
**or**
www.example.com/subdirABC/index.php

情况2

www.example.com/subdirXYZ/gallery/image.jpg
**to**
www.example.com/subdirXYZ/
**or**
www.example.com/subdirXYZ/index.php

2 个答案:

答案 0 :(得分:0)

.htaccess中尝试以下内容:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com
RewriteRule ^([^/]+/)gallery/[\w-]\.jpg$ /$1 [R,L]

对于所有符合模式/<subdir>/gallery/<filename>.jpg并且未与您的网站(example.com相关联)的请求,然后重定向到/subdir/

补充说明......

  • 直接链接(包括搜索引擎机器人)和无法发送HTTP Referer标头的用户代理也将被重定向到“页面”。

  • <filename>仅包含字符a-zA-Z0-9_-

    < / LI>
  • $1是对RewriteRule 模式中第一个捕获组的反向引用,即。 ([^/]+/) - 与您示例中的subdirXYZ/匹配。

  • 这是302(临时)重定向。

答案 1 :(得分:0)

它为我工作

RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_REFERER} !^https://example.com
RewriteRule ^(.*)/gallery/(.*.jpg) /$1/ [L,R=301]