0

我正在用 PHP 构建一个 MVC 框架。我正在使用这样的分段 URL:

http://mydomain.com/controller/method/param1/param2

我在 Apache .htaccess 中使用以下重写规则:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z\-_]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&method=$2&param1=$3&param2=$4 [NC,L]

URL 工作正常,并且正在调用正确的控制器和方法。但是,我在 apache2 error.log 中遇到了很多错误。每当我访问包含两个或更多段的 URL 时,我都会收到“找不到文件”错误。浏览器中没有发生 404。

澄清一下,以下 URL 不会导致 Apache 日志中出现错误。

http://dev.bonesphp.com/about

但是,以下 URL 会导致错误:

http://dev.bonesphp.com/about/test/pre >

文件不存在:/var/www/vhosts/dev.bonesphp.com/about

我的 php.ini 错误设置设置为 E_ALL / STRICT

有谁知道如何解决这一问题?

提前致谢!

更新:2013 年 5 月 12 日

我在 Apache 中启用了重写日志。这是我尝试访问上述 URL 时重写日志所说的内容:

127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747170a0/initial] (3) [perdir /var/www/vhosts /dev.bonesphp.com/] 添加路径信息后缀:/var/www/vhosts/dev.bonesphp.com/about -> /var/www/vhosts/dev.bonesphp.com/about/test 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747170a0/initial] (3) [perdir /var/www/vhosts /dev.bonesphp.com/] 去除每个目录前缀:/var/www/vhosts/dev.bonesphp.com/about/test -> about/test 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747170a0/initial] (3) [perdir /var/www/vhosts /dev.bonesphp.com/] 应用模式 '^([a-zA-Z\-_]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9] *)?/?([a-zA-Z0-9]*)?/?$' 到 uri 'about/test' 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747170a0/initial] (2) [perdir /var/www/vhosts /dev.bonesphp.com/] 重写 'about/test' -> 'index.php?controller=about&method=test¶m1=¶m2=' 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747170a0/initial] (3) 拆分 uri=index.php?controller =about&method=test¶m1=¶m2= -> uri=index.php, args=controller=about&method=test¶m1=¶m2= 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747170a0/initial] (3) [perdir /var/www/vhosts /dev.bonesphp.com/] 添加每个目录前缀:index.php -> /var/www/vhosts/dev.bonesphp.com/index.php 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747170a0/initial] (2) [perdir /var/www/vhosts /dev.bonesphp.com/] 去除 document_root 前缀:/var/www/vhosts/dev.bonesphp.com/index.php -> /index.php 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747170a0/initial] (1) [perdir /var/www/vhosts /dev.bonesphp.com/] 内部重定向与 /index.php [内部重定向] 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b6e4a3508/initial/redir#1] (3) [perdir /var /www/vhosts/dev.bonesphp.com/] 去除每个目录前缀:/var/www/vhosts/dev.bonesphp.com/index.php -> index.php 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b6e4a3508/initial/redir#1] (3) [perdir /var /www/vhosts/dev.bonesphp.com/] 应用模式 '^([a-zA-Z\-_]*)/?([a-zA-Z]*)?/?([a-zA- Z0-9]*)?/?([a-zA-Z0-9]*)?/?$' 到 uri 'index.php' 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b6e4a3508/initial/redir#1] (3) [perdir /var /www/vhosts/dev.bonesphp.com/] 去除每个目录前缀:/var/www/vhosts/dev.bonesphp.com/index.php -> index.php 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b6e4a3508/initial/redir#1] (3) [perdir /var /www/vhosts/dev.bonesphp.com/] 应用模式 '(^|/)\.' 到 uri 'index.php' 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b6e4a3508/initial/redir#1] (1) [perdir /var /www/vhosts/dev.bonesphp.com/] 通过 /var/www/vhosts/dev.bonesphp.com/index.php 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747130a0/initial] (3) [perdir /var/www/vhosts /dev.bonesphp.com/] 添加路径信息后缀:/var/www/vhosts/dev.bonesphp.com/about -> /var/www/vhosts/dev.bonesphp.com/about/favicon.ico 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747130a0/initial] (3) [perdir /var/www/vhosts /dev.bonesphp.com/] 去除每个目录前缀:/var/www/vhosts/dev.bonesphp.com/about/favicon.ico -> about/favicon.ico 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747130a0/initial] (3) [perdir /var/www/vhosts /dev.bonesphp.com/] 应用模式 '^([a-zA-Z\-_]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9] *)?/?([a-zA-Z0-9]*)?/?$' 到 uri 'about/favicon.ico' 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747130a0/initial] (3) [perdir /var/www/vhosts /dev.bonesphp.com/] 添加路径信息后缀:/var/www/vhosts/dev.bonesphp.com/about -> /var/www/vhosts/dev.bonesphp.com/about/favicon.ico 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747130a0/initial] (3) [perdir /var/www/vhosts /dev.bonesphp.com/] 去除每个目录前缀:/var/www/vhosts/dev.bonesphp.com/about/favicon.ico -> about/favicon.ico 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747130a0/initial] (3) [perdir /var/www/vhosts /dev.bonesphp.com/] 应用模式 '(^|/)\.' 到 uri 'about/favicon.ico' 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747130a0/initial] (1) [perdir /var/www/vhosts /dev.bonesphp.com/] 通过 /var/www/vhosts/dev.bonesphp.com/about 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b6e4a1290/initial/redir#1] (3) [perdir /var /www/vhosts/dev.bonesphp.com/] 去除每个目录前缀:/var/www/vhosts/dev.bonesphp.com/404.html -> 404.html 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b6e4a1290/initial/redir#1] (3) [perdir /var /www/vhosts/dev.bonesphp.com/] 应用模式 '^([a-zA-Z\-_]*)/?([a-zA-Z]*)?/?([a-zA- Z0-9]*)?/?([a-zA-Z0-9]*)?/?$' 到 uri '404.html' 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b6e4a1290/initial/redir#1] (3) [perdir /var /www/vhosts/dev.bonesphp.com/] 去除每个目录前缀:/var/www/vhosts/dev.bonesphp.com/404.html -> 404.html 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b6e4a1290/initial/redir#1] (3) [perdir /var /www/vhosts/dev.bonesphp.com/] 应用模式 '(^|/)\.' 到 uri '404.html' 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b6e4a1290/initial/redir#1] (1) [perdir /var /www/vhosts/dev.bonesphp.com/] 通过 /var/www/vhosts/dev.bonesphp.com/404.html
4

1 回答 1

1

我终于解决了这个问题!“文件不存在”错误是由浏览器对 favicon.ico 的请求引起的!

当 URL 只有一个段例如http://dev.bonesphp.com /about时,浏览器会从网站的根目录请求图标,这正是我的图标所在的位置。但是,如果 URL 有两个部分,例如http://dev.bonesphp.com /about/test,浏览器会从子目录中请求它。在上述 URL 的情况下,它会在http://dev.bonesphp.com/about中查找网站图标。这会导致 404,因为没有名为“about”的目录(“/about”映射到名为 About.php 的控制器文件)。

在 Google 上搜索后,我发现了适用于从任何目录请求的网站图标的 Apache 重写规则:

http://perishablepress.com/redirect-all-requests-for-a-nonexistent-file-to-the-actual-file/

多么好的PIA!我很高兴这一切都结束了!

于 2013-05-12T23:44:57.463 回答