htaccess 和 PHP 之间的重定向循环

发布于 2024-12-14 00:12:54 字数 821 浏览 0 评论 0原文

这是我的文件夹结构:

---- 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 1index.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-12-21 00:12:54

如果 HTTP 引荐来源网址不以 index.php 结尾,

!^.*index\.php$

您将执行重定向。然而,在您的问题中,您说过,引荐来源网址以 downloads/download 1 结尾。

HTTP_REFERER 由您的浏览器设置。并且您的浏览器不会更新该 URL。

除此之外,即使您的浏览器已经这样做,URL 仍然不会以 index.php 结尾,而是以 index.php?uid=downloads%2Fdownload%201 结尾。

因此,您必须重新考虑如何检查引荐来源网址以及如何更新引荐来源网址。也许使用重定向会有所帮助:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^.*index\.php.*$ [NC]
RewriteCond $1 !^index.php$ [NC]
RewriteRule (.*) index.php?uid=$1 [R,L]

In case the HTTP referrer does not end with index.php

!^.*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 with index.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:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^.*index\.php.*$ [NC]
RewriteCond $1 !^index.php$ [NC]
RewriteRule (.*) index.php?uid=$1 [R,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文