使用.htaccess隐藏文件

比方说,我有我的公共目录中的index.php文件。

使用DirectoryIndex index.php ,我可以将该文件设置为我的网站中的默认索引。

但是如果我不希望用户通过www.example.com/index.php进入, 只能通过 www.example.com进入,我该怎么做?

编辑

我添加了下面的代码:

 if (strpos($_SERVER['REQUEST_URI'], 'index.php')) { readfile('404.html'); exit; } 

index.php的开头,这样通过www.example.com/index.phpinput会导致404错误,但是通过www.example.cominput不会(因此,不会给潜在的攻击者提供信息,使用PHP或不)。 不是世界上最好的解决scheme,但确实有效。

你的问题没有意义。 如果您想向要求www.example.com的用户显示网页,则必须有某种types的DirectoryIndex 。

 DirectoryIndex index.php index.html 

会导致apachesearchindex.php的服务器的DocumentRoot ,然后index.html,并呈现为用户find的第一个渲染版本。

你不能真正“隐藏”页面。 您可以使用htaccess拒绝访问(导致403错误)。 你也可以使用mod_rewrite,从/index.php(如build议)到/或从/index.phpredirect到你的404文件(就像你现在所做的那样)。 隐藏页面最接近的是使用mod_rewrite从index.php到/,所以当用户inputwww.example.com或www.example.com/index.php时,它显示为www.example。 COM

你可以“禁止”/index.php类似的东西

 <Location /index.php> Order deny,allow Deny from all </Location> 

或者从/index.phpredirect到/ with mod_rewrite(这对用户更友好)。

我在你对Koos的评论中看到你想要隐藏index.php,这是你不能做的。 你最好的做法是把你想隐藏的部分放在另一个文件中,并将该文件包含在index.php中。 完成正确的用户将不会看到包含声明,也不包含文件,只有结果输出。