如何使用.htaccess从私有目录安全地重写为index.php

时间:2016-07-06 21:26:29

标签: php apache .htaccess mod-rewrite

我有以下重写规则:

############################################
## never rewrite for existing files, directories and links

     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_FILENAME} !-l

############################################
## rewrite everything else to index.php

        RewriteRule .* index.php [L]

我有一个名为“app”的目录,在尝试访问时会抛出apache的默认禁止访问错误。

在我的PHP应用程序中,我有一个我想要定向到我的index.php的路由 - app/help。如果我使用index.php/app/help,路由将加载,因为我直接通过index.php发送它。如果我尝试删除index.php并调用路由app/help,我会收到一个禁止访问错误,因为我的规则表明不应该访问该目录。

如何在保持我的应用程序目录内容的同时安全地使用“假路径”可以安全地使用apache?

1 个答案:

答案 0 :(得分:0)

重写规则从上到下进行评估。因此,您需要做的就是插入重写规则,该规则将重定向/app/help ,然后服务器遇到任何禁止访问该路径的规则:

RewriteRule ^app/help   index.php [END]

[END]告诉服务器停止所有重写。基本上index.php是最终目的地。如果你有一个非常旧版本的Apache,你可以使用[L]标志而不是最终标志,但除非你有另一条规则将index.php重定向到另一条路径,否则它将起作用。