mod-rewrite重定向但阻止直接访问 [英] mod-rewrite redirect but prevent direct access

查看:54
本文介绍了mod-rewrite重定向但阻止直接访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将所有内容重定向到:

I want to redirect all content to:

www.example.com/public/...

但禁止直接访问

www.example.com/public/file1/
www.example.com/public/file2/
etc

最终到达网址应为:

www.example.com/file1/

我已经尝试过进行重定向,并且可以正常工作-但我不知道如何防止直接访问:

I've tried this for redirecting and it works - but I dont know how to prevent direct access:

ReWriteCond %{REQUEST_URI} !/public/
RewriteRule ^(.*) public/$1 [L]

推荐答案

花了无数时间尝试解决此问题后,我发现解决方案在于文档不足的

After spending an inordinate amount of time trying to solve this problem, I found that the solution lies with the under-documented REDIRECT_STATUS environment variable.

将此代码添加到顶级/.htaccess代码的开头,以及添加到其下的所有.htaccess文件(例如/public/.htaccess):

Add this to the beginning of your top-level /.htaccess code, and also to any .htaccess files you have under it (e.g. /public/.htaccess):

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteRule ^ /public%{REQUEST_URI} [L]

</IfModule>

现在,如果用户请求example.com/file1,则会向他们提供/public/file1处的文件.但是,如果他们直接请求example.com/public/file1,则服务器将尝试在/public/public/file1提供文件,这将失败(除非您碰巧在该位置有文件).

Now, if the user requests example.com/file1 then they are served the file at /public/file1. However, if they request example.com/public/file1 directly then the server will attempt to serve the file at /public/public/file1, which will fail (unless you happen to have a file at that location).

重要提示:

您需要将这些行添加到所有.htaccess文件中,而不只是添加到Web根目录中的顶层目录,因为如果您在Web根目录下有任何.htaccess文件(例如/public/.htaccess),则这些将覆盖顶级.htaccess ,而用户将再次能够直接访问/public中的文件.

You need to add those lines to all .htaccess files, not just the top-level one in the web root, because if you have any .htaccess files below the web root (e.g. /public/.htaccess) then these will override the top-level .htaccess and users will again be able to access files in /public directly.

有关变量和重定向的说明:

执行重定向(或重写)使整个过程重新从新URI开始,因此任何变量您在重定向之前设置的内容将不再在之后设置.这样做是有意进行的,因为通常您不希望最终结果取决于您到达那里的方式(即是通过直接请求还是通过重定向).

Performing a redirect (or a rewrite) causes the whole process to start again with the new URI, so any variables that you set before the redirect will no longer be set afterwards. This is done deliberately, because usually you do not want the final result to depend on how you got there (i.e. whether it was via a direct request or via a redirect).

但是,对于那些想要做的特殊场合,您可以使用REDIRECT_STATUS.另外,任何在重定向之前设置的环境变量(例如,使用 SetEnvIf )在重定向后仍然可用,但带有前缀为变量的名称(因此MY_VAR变为REDIRECT_MY_VAR).

However, for those special occasions where you do want to know how you got to a particular URI, you can use REDIRECT_STATUS. Also, any environment variables set before the redirect (e.g. with SetEnvIf) will still be available after the redirect, but with REDIRECT_ prefixed to the name of the variable (so MY_VAR becomes REDIRECT_MY_VAR).

这篇关于mod-rewrite重定向但阻止直接访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆