htaccess 和 PHP 之间的重定向循环
这是我的文件夹结构:
---- downloads
---- .htaccess
---- index.php
---- download 1
---- download 2
---- download n
.htaccess 内容:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^.*index\.php$ [NC]
RewriteCond $1 !^index.php$ [NC]
RewriteRule (.*) index.php?uid=$1 [L]
index.php 内容:
<?php
// do stuff
header('Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
?>
我将每个下载请求重定向,例如 downloads/download 1
到 index.php
,我在其中执行某些操作,然后重定向到原始文件 downloads/download 1
。
效果很好,只是我不知道当 HTTP 引荐来源网址为 index.php
时如何不在 .htaccess
中应用重写规则。
我尝试使用 RewriteCond %{HTTP_REFERER} !^.*index\.php$ [NC]
但它一直循环。
This is my folder structure:
---- downloads
---- .htaccess
---- index.php
---- download 1
---- download 2
---- download n
.htaccess contents:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^.*index\.php$ [NC]
RewriteCond $1 !^index.php$ [NC]
RewriteRule (.*) index.php?uid=$1 [L]
index.php contents:
<?php
// do stuff
header('Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
?>
I'm redirecting every request for a download, e.g. downloads/download 1
to index.php
where I do something and then redirect to the original file, downloads/download 1
.
This works out fine except that I don't know how to NOT apply the rewrite rule in .htaccess
when the HTTP referrer is index.php
.
I tried with RewriteCond %{HTTP_REFERER} !^.*index\.php$ [NC]
but it keeps looping.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 HTTP 引荐来源网址不以
index.php
结尾,您将执行重定向。然而,在您的问题中,您说过,引荐来源网址以
downloads/download 1
结尾。HTTP_REFERER
由您的浏览器设置。并且您的浏览器不会更新该 URL。除此之外,即使您的浏览器已经这样做,URL 仍然不会以
index.php
结尾,而是以index.php?uid=downloads%2Fdownload%201
结尾。因此,您必须重新考虑如何检查引荐来源网址以及如何更新引荐来源网址。也许使用重定向会有所帮助:
In case the HTTP referrer does not end with
index.php
you do the redirect. In your question however you've said, that the referrers ends with
downloads/download 1
.The
HTTP_REFERER
is set by your browser. And your browser will not update the URL.Next to that, even if your browser would have done, the URL would still not end with
index.php
but withindex.php?uid=downloads%2Fdownload%201
.So you have to re-think how you would like to check for the referrer and how you update the referrer as well. Maybe using a redirect helps with that: