Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 从一个子目录重定向到另一个子目录,但阻止对后一个子目录的直接访问_Apache_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

Apache 从一个子目录重定向到另一个子目录,但阻止对后一个子目录的直接访问

Apache 从一个子目录重定向到另一个子目录,但阻止对后一个子目录的直接访问,apache,.htaccess,mod-rewrite,redirect,Apache,.htaccess,Mod Rewrite,Redirect,正如标题所暗示的,我想将一个子目录重定向到另一个子目录,同时阻止对后一个子目录的直接访问。例如,我想阻止直接访问domain.com/foo: RewriteRule ^foo [F,NC] RewriteRule ^bar/* /foo/ [R=301] 但是我想domain.com/bar重定向到domain.com/foo: RewriteRule ^foo [F,NC] RewriteRule ^bar/* /foo/ [R=301] 有没有可能把这两条规则结合在一起,因此dom

正如标题所暗示的,我想将一个子目录重定向到另一个子目录,同时阻止对后一个子目录的直接访问。例如,我想阻止直接访问
domain.com/foo

RewriteRule ^foo [F,NC]
RewriteRule ^bar/* /foo/ [R=301]
但是我想
domain.com/bar
重定向到
domain.com/foo

RewriteRule ^foo [F,NC]
RewriteRule ^bar/* /foo/ [R=301]

有没有可能把这两条规则结合在一起,因此
domain.com/bar
将加载
domain.com/foo
,但
domain.com/foo
将不会加载
domain.com/foo

通过
httpd.conf
启用mod_rewrite和.htaccess,然后将此代码放入
文档根目录下的
htaccess

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# block direct access to /foo/*
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+foo[/\s] [NC]
RewriteRule ^ - [L,F]

# allow access to /foo/* via /bar/*
RewriteRule ^bar/(.*)$ /foo/$1 [L,NC]

这很好用。请您快速解释一下它是如何工作的好吗?当然,第一条规则是使用
%{THE_REQUEST}
变量来检查浏览器中输入的实际URL是否为
/foo/*
,如果它是禁止的,则会将错误返回给用户。第二条规则是在内部将所有
/bar/anything
请求转发到
/foo/anything
URI。由于第二条规则中未使用
R
标志,因此
%{THE_REQUEST}
不会更改,并且不会再次触发第一条规则。如果它回答了你的问题,你现在能接受这个答案吗?这个问题已经结束了?教科书上的例子,无聊的管理员没有更好的事情做。