0

I'm building an MVC framework in PHP. I'm using segmented URLs like so:

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

I'm using the following rewrite rules in 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]

The URLs work fine and the correct controllers and methods are being called. However, I'm getting lots of errors in the apache2 error.log. Anytime I access a URL with two or more segments I get a "file not found" error. There are no 404s happening in the browser.

Just to clarify, the following URL does not cause errors in the Apache log.

http://dev.bonesphp.com/about

However, the following URL will cause an error:

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

File does not exist: /var/www/vhosts/dev.bonesphp.com/about

My php.ini error settings are set to E_ALL / STRICT

Does anyone know how to fix this?

Thanks in advance!

UPDATE: 5/12/2013

I enabled the rewrite log in Apache. This is what the rewrite log says when I try to access the above 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/] add path info postfix: /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/] strip per-dir prefix: /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/] applying pattern '^([a-zA-Z\-_]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$' to 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/] rewrite 'about/test' -> 'index.php?controller=about&method=test&param1=&param2=' 127.0.0.1 - - [12/May/2013:17:52:35 --0400] [dev.bonesphp.com/sid#7f0b748dc0a8][rid#7f0b747170a0/initial] (3) split uri=index.php?controller=about&method=test&param1=&param2= -> uri=index.php, args=controller=about&method=test&param1=&param2= 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/] add per-dir prefix: 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/] strip document_root prefix: /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/] internal redirect with /index.php [INTERNAL REDIRECT] 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/] strip per-dir prefix: /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/] applying pattern '^([a-zA-Z\-_]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$' to 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/] strip per-dir prefix: /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/] applying pattern '(^|/)\.' to 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/] pass through /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/] add path info postfix: /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/] strip per-dir prefix: /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/] applying pattern '^([a-zA-Z\-_]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$' to 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/] add path info postfix: /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/] strip per-dir prefix: /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/] applying pattern '(^|/)\.' to 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/] pass through /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/] strip per-dir prefix: /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/] applying pattern '^([a-zA-Z\-_]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$' to 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/] strip per-dir prefix: /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/] applying pattern '(^|/)\.' to 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/] pass through /var/www/vhosts/dev.bonesphp.com/404.html
Neil Girardi
  • 4,533
  • 1
  • 28
  • 45
  • you have `¶m1` instead of `&m1` maybe a typo. Try to add a slash before index.php: `?$ /index.php?` – Bass Jobsen May 08 '13 at 10:01
  • @Bass Jobson - Thanks for the reply. Those aren't typos. Some of the text got garbled when I copied and pasted the rewrite rule into my original post. I edited the post to correct it. – Neil Girardi May 08 '13 at 22:11

1 Answers1

1

I finally solved this! The "File does not exist" errors were caused by browser requests for favicon.ico!

When the URL has only one segment such as http://dev.bonesphp.com/about, the browser requests the favicon from the website's root directory, which is exactly where my favicon is located. However, if the URL has two segments such as http://dev.bonesphp.com/about/test, the browser requests it from a sub-directory. In the case of the above mentioned URL it looks for the favicon in http://dev.bonesphp.com/about. This results in an 404 since there's no directory called 'about' ('/about' maps to a controller file called About.php).

After searching on Google I found an Apache rewrite rule that works for favicons requested from any directory:

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

What a PIA! I'm so glad this is over!

Neil Girardi
  • 4,533
  • 1
  • 28
  • 45