How do MVC systems where the urls are in these forms force all the requests through a single index.php file?


EDIT: When I try the rewrite rules below I get this error:

[error] [client] Invalid URI in request GET / HTTP/1.1
[error] [client] Invalid URI in request GET /abc HTTP/1.1

EDIT: Oh, this is the complete content of /index.php. When I remove the rewrite rules, it outputs '/' or '/index.php' or I get a 404 for anything else.

echo htmlspecialchars($_SERVER['REQUEST_URI']);

SOLVED: I added a / in front of index.php in the rewrite rule and then it worked:

SOLVED AGAIN: Turns out the / was only needed because I was running 2.2.4. When I upgraded to 2.2.11 the / was no longer needed.

  • Which set of rules did you use? I've tried both of them and both are working well. Are you sure you have mod_rewrite activated? – Manuel Faux Jul 26 '09 at 19:29
  • Server would not start if mod_rewrite weren't active. This is Apache 2.2 on WinXP if that matters. – jmucchiello Jul 26 '09 at 19:31

If you are using Apache, use rewrites via mod_rewrite:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]

This will rewrite your URLs to »index.php?q=foo/bar/baz« transparently.

The 2. and 3. lines tell the rewrite engine not to rewrite the URL if it points to an existing file or directory. This is necessary to have access to real files provided by the httpd server.

| improve this answer | |
  • See edit above. Getting error 400. – jmucchiello Jul 26 '09 at 14:27
  • What was the exact request you sent to the server? Does your server have mod_rewrite installed? I do guess so, because otherwise it won't start because of an syntax error. – Manuel Faux Jul 26 '09 at 15:26
  • The edit shows the exact request: localhost:8181 and localhost:8181/abc. If I remove the rewrite rules I get my index.php with / and a normal 404 with /abc. – jmucchiello Jul 26 '09 at 19:12
  • I added a / in front of index.php and that fixed the problem. – jmucchiello Jul 26 '09 at 19:37
  • Mysterious... I've tested without the slash and it worked for me with apache-2.2.11-8. – Manuel Faux Jul 26 '09 at 20:05

The code below uses Apache's mod_rewrite to rewrite all URLs not pointing to a physical file or directory to be mapped to index.php. The end result will be:


Rewrite rule:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [PT,L,QSA]


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

Both the above lines dictate that this rule does not apply to regular files (-f) or directories (-d).

RewriteRule ^(.*)$ index.php/$1 [PT,L,QSA]

More information on how to create mod_rewrite rules can be gathered from the Apache web site: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html.

| improve this answer | |
  • See edit above. Getting error 400. – jmucchiello Jul 26 '09 at 14:28
  • I wish I could set both answers as correct. – jmucchiello Jul 26 '09 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.