Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

http://www.example.com/foo/bar/baz
http://www.example.com/goo/car/caz/SEO-friendly-name-of-the-object
http://www.example.com/hey/you

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

[error] [client 127.0.0.1] Invalid URI in request GET / HTTP/1.1
[error] [client 127.0.0.1] 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.

<?php
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.

share|improve this question
    
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

2 Answers 2

up vote 6 down vote accepted

If you are using apache, use 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.

share|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:

http://www.example.com/index.php/foo/bar/baz
http://www.example.com/index.php/goo/car/caz/SEO-friendly-name-of-the-object
http://www.example.com/index.php/hey/you

Rewrite rule:

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

Explanation:

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.

share|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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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