PHP目录重写规则

PHP目录重写规则,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,我的MVC php应用程序具有以下结构 / /Models/ /Views/ /Content/ /libs/ index.php <-- Controllers admin.php blog.php / /模型/ /观点/ /内容/ /自由基/ index.php/index.php/index.php/index.php不是有效路径。根/index.php仍然是服务器正在运行的根。但是,浏览器没有足够的智能来了解这一点,因此,如果css和图像文件的路径是相对的,那么它现在会

我的MVC php应用程序具有以下结构

/

/Models/
/Views/
/Content/
/libs/

index.php    <-- Controllers
admin.php
blog.php
/
/模型/
/观点/
/内容/
/自由基/

index.php
/index.php/index.php/index.php
不是有效路径。根
/index.php
仍然是服务器正在运行的根。但是,浏览器没有足够的智能来了解这一点,因此,如果css和图像文件的路径是相对的,那么它现在会在错误的位置查找它们,这就是页面无法正确呈现的原因

就服务器而言,
www.test.com/index.php/index.php/index.php
与调用
www.test.com/index.php

但是对于浏览器来说,如果你在html标题
中有它,它现在正试图在
www.test.com/index.php/index.php/css/style.css
中找到它,当然它在那里并不存在

基本上我希望人们允许根目录中的acces.php文件。(index.php,admin.php)

使用
mod\u rewrite
代替
指令,以实现更精细的控制:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(index|admin)\.php$ [NC]
RewriteCond %{THE_REQUEST} \.php[\s?/] [NC]
RewriteRule ^ - [F,NC]
出于某种奇怪的原因,人们可以访问像index.php/index.php/index.php这样的URL

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

<Files index.php>
Order Allow,Deny
Allow from all
</Files>
这不是一个BUG,而是一个特性;)

这实际上是apache的一个特性。Apache允许在脚本名称后添加路径信息。在php中,您可以从$\u服务器['PATH\u INFO']访问此值 URL index.php/a/b将运行脚本index.php,您将在PATH_INFO中获得“a/b”

如何禁用此类型的URL

要禁用此类url,需要在apache配置中禁用“AcceptPathInfo”

<Files "*.php">
  AcceptPathInfo Off
</Files> 

AcceptPathInfo关闭

您可以在

Ok中找到有关AcceptPathInfo的更多信息,那么解决方案是什么?我想把*.php/之后的任何内容重定向到使用anubhava建议的mod_rewrite。好吧,这是为了白名单允许的php文件,但人们仍然可以访问index.php/{anythinghere}或admin.php/{anythinghere}甚至index.php///index.php和admin.php上运行的代码也没有关系,因为没有安全问题。如果您担心有人真的这样做,并且它在您的html中使用了不正确的绝对路径。或者如果您真的担心这样做,如果他们不在某个白名单文件中,请使用重定向而不是重写。必须有某种重写规则:site.com/index.php{拒绝此处插入的所有内容,包括前斜杠}如果您得到了我想要实现的内容。请立即尝试更新的代码,它将立即阻止
/index.php
之后的任何内容。