将htaccess传递给PHP文件

时间:2015-01-19 10:58:54

标签: php .htaccess

我想创建系统,它会在进入用户之前检查文件。

这是我在.htaccess档案中的主线:

RewriteRule ^([^\.]+)$ ./getfile.php?hash=$1 [L,QSA]

这是getfile.php档案:

print_r($_GET['hash']);

这两个文件都位于名为get的单独文件夹中。

所以,当我尝试这个网址时:

http://localhost/example/get/information

然后打印information - 这就是我所期待的。

但是,当我尝试此网址时(可能会有更多information个文件):

http://localhost/example/get/information.txt

这会打印错误404.我的文件存储在安全文件夹中,因此没有对任何文件的直接访问权限。例如,只有登录的用户才能下载这些文件。

那么,如何修改任何文件,$_GET['hash']将包含information.txt

PS:不要给我更好的方法,我想这样做。

1 个答案:

答案 0 :(得分:0)

您可以使用以下命令替换规则:

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ getfile.php?hash=$1 [L,QSA]

你的正则表达式[^/.]+只是在DOT之前抓取文字。