【问题标题】:How can I redirect a request based on the HTTP Referrer?如何根据 HTTP Referrer 重定向请求?
【发布时间】:2019-06-04 17:39:06
【问题描述】:

我有一个重定向规则,可以将所有 PDF、Word、Excel 和 Power point 文档重定向到一个页面。

RewriteEngine on
RewriteRule ^(.*).(pdf|doc|docm|docx|dot|dotm|dotx|odt|csv|dbf|dif|xls|xlsb|xlsm|xlsx|xlt|xltm|xltx|xlw|pot|potm|potx|ppa|ppam|pps|ppsm|ppsx|ppt|pptm|pptx)$ http://myurl.ca/file-download-tracker/?file=$1.$2 [R,L]

在该页面上,我有一些代码将有关此文件的数据推送到 Google Analytics,然后重定向回用户最初尝试访问的文件。

我正在尝试为此规则编写一个条件,如果用户不是来自http://myurl.ca/file-download-tracker/?file=myfile.pdf url 但没有运气,它将只重定向这些文件。这条规则怎么写?

这是我认为我在这种情况下得到的最接近的情况:

RewriteCond %{HTTP_REFERER} !^http://myurl.ca/file-download-tracker/.*

【问题讨论】:

    标签: apache .htaccess redirect


    【解决方案1】:

    我想出了一个可行的条件,它能够检查引用者是否来自“file-download-tracker”页面。

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^[^?]+\?([^&]*&)*file-download-tracker(.*)file=(.*)(&.*)?$
    RewriteRule ^(.*).(pdf|doc|docm|docx|dot|dotm|dotx|odt|csv|dbf|dif|xls|xlsb|xlsm|xlsx|xlt|xltm|xltx|xlw|pot|potm|potx|ppa|ppam|pps|ppsm|ppsx|ppt|pptm|pptx)$ http://my-url.ca/file-download-tracker/?file=$1.$2 [L,NC,R=302,E=nocache:1]
    

    现在我正在与浏览器缓存作斗争。重定向到此页面后,浏览器会缓存该重定向,因此当我尝试返回用户单击的原始文件时,浏览器会将我带回重定向页面。所以它现在陷入了一个循环......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      相关资源
      最近更新 更多