dongqi0644 2017-06-22 15:32
浏览 58
已采纳

将子文件夹中的文件直接重定向到脚本

I have a subfolder that holds user uploaded files. I want to redirect all direct file requests to this folder to another .php script...so i can check if the user is logged in before i send/show the file to him. For example:

/mainsite/uploads/user/2324/file.pdf

needs to be forwarded to

/mainsite/uploads/permissions.php

But i need inside the permissions.php to be able to do:

$url = $_SERVER['REQUEST_URI'];

and see what was the initial request...in order to readfile() the file after all the 'checking'.

I've tried this:

RewriteEngine On
RewriteRule ^/uploads/user/?$ /uploads/permissions.php [R=301,L]

but this is just a simple forwarding...i got no idea what file or folder the user requested.

I know i can do this by creating an individual htaccess file inside every folder that is created under 'user/{userid}' but i wanted a simpler function. I dont want to have 10000 htaccess files, if i can do this with just one.

Thanks

  • 写回答

1条回答 默认 最新

  • duanhongqiong9460 2017-06-22 15:36
    关注

    try with this syntax: (i added the R=301 part, which wasn't necessary in my version, so it is not fully tested, works without the R option)

    RewriteRule "^/uploads/user/(.+)$" "/uploads/permissions.php?file=$1" [R=301,QSA,L]
    

    You can the get your file var in the $_GET array in permissions.php. However, i wouldn't recommend to use directly this value because it can be unsecure. The best way is to only allow fixed values, with a switch for example, having filtered the var as a string before.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容