【问题标题】:.htaccess redirection for image link.htaccess 重定向图片链接
【发布时间】:2017-10-01 19:44:14
【问题描述】:

我想知道是否可以在 .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

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    它适合我

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

    【讨论】:

    • 这和我在回答中已经写的差不多??
    • @MrWhite 不幸的是,您的代码对我不起作用,无论如何感谢您为开发我的新代码提供一些想法
    【解决方案2】:

    .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_-

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 2012-03-20
      • 2018-07-15
      • 2012-04-19
      相关资源
      最近更新 更多