我的文件夹结构看起来是这样的。
-β
+文件夹1
+ FOLDER2
+ folder3
file1.php
file2.php
现在我想限制所有文件和文件夹,但不能FOLDER2和file2.php 我怎么能写我的.htaccess做到这一点。这是我写的。
选项+了FollowSymLinks
RewriteEngine叙述上
的RewriteCond%{REQUEST_URI}!/ FOLDER2%$
的RewriteCond%{REQUEST_URI}!/file2.php
重写规则$ /beta/file2.php [R = 302,L]
不知怎的,上面确实在一个系统工作,但不能从其他系统。你能指出我在做什么错在这里?
谢谢
萨哈
解决方案试着改变周围的规则,以便让你的2个文件夹穿过然后重定向一切文件2:
选项+了FollowSymLinks
RewriteEngine叙述上
的RewriteBase /测试/
重写规则^文件2 的.php - [L]
重写规则^ FOLDER2 - [L]
重写规则^ file2.php [L,R = 302]
您是正的错误可能是从%
,你有你的状态。
如果你想彻底禁止访问别的,你可以改变的最后一个规则:
重写规则^ - [L,F]
如果你不介意,你能解释一下每一行?
在的RewriteBase
指令让重写引擎知道相对路径有一个URI基的 /测试/
。因此,所有的文件/路径名不开始与 /
将自动获得该基地。
接下来的两个规则只是对匹配的URI,并做了穿越使用 -
字符,这意味着,什么也不做。所以,当你要求 / FOLDER2
或 /file2.php
,重写引擎不执行任何操作,并允许通过,如果该请求什么都没发生。但是,如果请求是其他任何东西,第一条规则将不匹配,最后一个规则将匹配,因为正则表达式是 ^
,它匹配一切。该规则的目标重定向(因基)一切 /beta/file2.php
。
与 F
标志中的方括号中的禁止规则是一样的东西,如果请求不是为FOLDER2或file2.php,则该规则将与之相匹配的( ^
),并使其通过( -
),但 F
标记使得它返回一个403禁止,而不是服务于任何请求是为。
My folder structure looks something like this.
-beta
+folder1
+folder2
+folder3
file1.php
file2.php
Now i want to restrict all files and folders but not folder2 and file2.php How can i write my .htaccess to do this. This is what i have written.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/folder2%$
RewriteCond %{REQUEST_URI} !/file2.php
RewriteRule $ /beta/file2.php [R=302,L]
Somehow the above does work in one system but not from other system. Can you point out what i am doing wrong here ?
Thanks,
Saha
解决方案Try changing your rules around so that you let the 2 folders pass through then redirect everything to file2:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /beta/
RewriteRule ^file2.php - [L]
RewriteRule ^folder2 - [L]
RewriteRule ^ file2.php [L,R=302]
The error you were getting is probably from the %
that you had in your condition.
If you want to outright forbid access to anything else, you can change the last rule to:
RewriteRule ^ - [L,F]
If you dont mind can you please explain each line ?
The RewriteBase
directive lets the rewrite engine know that relative paths have a URI-base of /beta/
. So all of the files/pathnames that don't start with a /
will automatically have that base.
The next two rules simply matches against the URI and does a "pass through" using the -
character, which means, "don't do anything". So when you request /folder2
or /file2.php
, the rewrite engine does nothing and lets the request through as if nothing happened. But if the request is anything else, the first 2 rules won't match and the last rule will match because the regex is ^
, which matches everything. The target of that rule redirects everything to /beta/file2.php
(because of the base).
The forbidden rule with the F
flag in the square brackets is the same thing, If the request isn't for the folder2 or file2.php, then that rule will match it (^
) and it passes it through (-
) but the F
flag makes it return a "403 Forbidden" instead of serving whatever the request was for.
相关推荐
最新文章