htaccess选择性转发

发布于 2024-12-10 18:18:45 字数 855 浏览 0 评论 0原文

这是一个有点独特的问题,我似乎无法在任何地方找到答案!我是处理 htaccess 文件的新手。这就是我想要做的,让我知道这是否可能:

我试图在有人直接访问某个目录时进行重定向,但是,我的服务器需要能够访问该目录以启动文件下载。

这是我用来完成重定向的方法:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ ../ [NC]

它的工作原理就像一个魅力,但是当我的脚本尝试访问文件系统时,它自然也会被重定向。

这样做的目的是保护文件下载。用户通过保护区访问下载,下载链接类似于 http://example.com/download/superlongidhash< /a>

然后脚本会获取并读取该 URL,制定文件的实际路径并相应地发送 php 标头:

header("Content-type: application/force-download"); 
header("Content-Transfer-Encoding: Binary"); 
header("Content-length: ".filesize($path)); 
header("Content-disposition: attachment; filename=" . basename($path) . ""); 
readfile($path); 

任何帮助将不胜感激:) 如果有更好的方法来执行此操作,请告诉我!

仅供参考,这一切都在 CodeIgniter 框架内

This is somewhat of a unique question that I cant seem to find the answer for anywhere! I am new to playing with htaccess files. Here's what im trying to do, let me know if this is at all possible:

I am trying to redirect when someone access a certain directory DIRECTLY, however, my server needs the ability to access the directory to initiate file downloads.

Here's what I am using to accomplish the redirect:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ ../ [NC]

It works like a charm, however when my script tries to access the filesystem it naturally gets redirected as well.

The purpose of this is protected file downloads. The user's access downloads via a protected area and the download link is similar to http://example.com/download/superlongidhash

This URL is then taken and read by the script, formulates the actual path to the file and sends php header's accordingly:

header("Content-type: application/force-download"); 
header("Content-Transfer-Encoding: Binary"); 
header("Content-length: ".filesize($path)); 
header("Content-disposition: attachment; filename=" . basename($path) . ""); 
readfile($path); 

Any help would be much appreciated :) If there is a better way to do this, let me know!

FYI, this is all within the CodeIgniter framework

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

捎一片雪花 2024-12-17 18:18:45

您可以检查 http_referrer 是否适合您的网站。如果不是,您可以将用户重定向到您的主页(或您想要的任何地方)。

看看我发现的这篇关于做你想做的事情的文章。
http://www .dialme.com/m/articles/view/How-to-Prevent-direct-access-to-files-and-folders

我希望这会有所帮助。

You can check the http_referrer is the one for your site. If it's not, you redirect the user to your homepage (or wherever you want).

Take a look into this article I found about doing exactly what you want.
http://www.dialme.com/m/articles/view/How-to-Prevent-direct-access-to-files-and-folders

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文