简体   繁体   English

如何使用htaccess重写规则来重定向现有文件?

[英]How can I use an htaccess rewrite rule to redirect away from an existing file?

I have a directory called users which contains sub directories for each user. 我有一个名为用户的目录,其中包含每个用户的子目录。 Eg my directory structure might look like: 例如,我的目录结构可能类似于:

users/
    .htaccess
    UserAccess.php
    foo/
        baz.txt
    bar/
        passwd.txt

I want to prevent users from accessing other users files. 我想防止用户访问其他用户文件。 Therefore, I wrote a php script which checks the path and prints the file contents or not. 因此,我写了一个php脚本来检查路径并打印文件内容。 The problem is that the script is not being run, but rather apache is trying to access the files directly. 问题在于该脚本未在运行,而是apache尝试直接访问文件。

My .htaccess in the users/ directory is: 我的users。目录中的.htaccess是:

RewriteEngine on
RewriteRule ^users/ UserAccess.php

A user would then try to access http://mywebsite.com/users/username/file . 然后,用户将尝试访问http://mywebsite.com/users/username/file Eg http://mywebsite.com/users/foo/baz.txt . 例如http://mywebsite.com/users/foo/baz.txt

The key point is that http://mywebsite.com/users/username/ is a REAL directory. 关键是http://mywebsite.com/users/username/是一个REAL目录。

How do I fix this to accomplish what I want? 我该如何解决这个问题以完成我想要的?

EDIT: 编辑:

RewriteEngine on
RewriteRule . UserAccess.php

doesnt work either. 也不起作用。

if your URL is like http://example.com/users/... adding: 如果您的网址类似于http://example.com/users/...添加:

RewriteCond %{REQUEST_FILENAME} -f

should make it work. 应该使它工作。

I've set an example on my computer with the following .htaccess 我在计算机上使用以下.htaccess设置了示例

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^testdir readHTACCESS.php

and it works just fine... 而且效果很好...

LINK 链接

为了防止直接访问,您可以对UserAccess.php以外的所有文件Deny from all使用Deny from all

我猜你需要使用mod_rewrite的选项看看这里

I'll assume that mod_rewrite is alive and running on your server, and all we're dealing with is how to use it. 我假设mod_rewrite仍在运行并在您的服务器上运行,而我们所要处理的只是如何使用它。 :-) :-)

I suggest you specify an explicit path to UserAccess.php. 我建议您为UserAccess.php指定一个明确的路径。 Do you know what the "working directory" is for your rewrite rule? 您知道重写规则的“工作目录”是什么吗?

RewriteRule ^/users/ /users/UserAccess.php

I've tested this and it works for me. 我已经测试过了,对我有用。 If it doesn't for you, then perhaps there's a configuration problem that's not just the rewrite rule. 如果不适合您,那么可能存在配置问题,而不仅仅是重写规则。 If that's the case, it would be very helpful to know what you're seeing in your access.log and error.log. 如果是这样,知道在access.log和error.log中看到的内容将非常有帮助。

You can also perhaps simplify what's going on in PHP by feeding the filename as a variable: 您也可以通过将文件名作为变量输入来简化PHP中的操作:

RewriteRule ^/users/(.+\.txt) /users/UserAccess.php?what=$1

That way, you can skip parsing $_SERVER['REQUEST_URI'] and just test for the existence of the file prior to sending it out with readfile() or equiv. 这样,您可以跳过解析$ _SERVER ['REQUEST_URI']并仅在通过readfile()或equiv发送文件之前测试文件是否存在。

The answer was that the subfolder (foo in the example) had permissions 700. This caused a 403 response before the rewrite rule was parsed. 答案是子文件夹(示例中为foo)具有权限700。这在解析重写规则之前引起了403响应。 I changed the permissions to 701 and everything worked. 我将权限更改为701,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM