简体   繁体   English

使用Apache拒绝除索引以外的所有文件

[英]Deny all files except the index using apache

Here's my .htaccess 这是我的.htaccess

<Files *>
  Order Deny,Allow
  Deny from all
</Files>

<Files index.php>
  Order Deny,Allow
  Allow from all
</Files>

This is not working, cause if I type the hostname in my browser, it serves the index.php but apache doesn't seem to apply the Files instructions and instead returns a non-allowed file access page, I need typing the fullname document (eg 'index.php') to make it work. 这是行不通的,因为如果我在浏览器中输入主机名,它将提供index.php,但是apache似乎没有应用“文件”指令,而是返回了不允许的文件访问页面,我需要输入全名文档(例如“ index.php”)以使其正常工作。 which is not really convenient... 这不是很方便...

how to proceed if I want users only access index files of each folder in my website ? 如果我希望用户仅访问网站中每个文件夹的索引文件,该如何进行? all the other files are just script inclusions so i believe i'm doing right trying to make them inaccessible from the web (or maybe not, if only you have one reason to prove the other case). 所有其他文件都只是脚本包含的内容,因此我相信我正在做正确的尝试,以使它们无法从Web上访问(或者,如果您只有一个理由来证明另一种情况,也许不能)。

Regardless the question above, is it the right way to do the job ? 不管上面的问题,这是正确的方法吗? (I think the two directives here are not neat but it's the only way, well almost the only way that I know to avoid accesses to files). (我认为这里的两个指令并非整齐,但这是避免访问文件的唯一方法,几乎​​是我所知道的唯一方法)。

Not exactly sure why you need to do this, but you can use mod_setenvif (no need to wrap this inside a <Files> ) 不确定是否需要这样做,但是可以使用mod_setenvif (无需将其包装在<Files>

SetEnvIf Request_URI ^/index.php$ index
Order Allow,Deny
Allow from env=index

This will cause access to hostname.com/ to 403 but allow hostname.com/index.php. 这将导致对hostname.com/的访问达到403,但允许hostname.com/index.php。 If you want to allow / as well, just add 如果您也想允许/,只需添加

SetEnvIf Request_URI ^/$ index

to the top. 到顶部。 Of course, all this will make it so anything that index.php links to will also return a 403. 当然,所有这些将使index.php链接到的任何内容也将返回403。

<Files *?>
    Order deny,allow
    Deny from all
</Files>

You just need to add a question mark to match at least one character. 您只需要添加一个问号即可匹配至少一个字符。

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

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