How to config .htaccess to support MVC?

Artisan picture Artisan · Jul 19, 2012 · Viewed 7.1k times · Source

I would like to write PHP MVC web application.

For now I'm trying to route any typed in URL to index.php so I created a .htaccess file as followed

RewriteEngine On

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

RewriteRule ^(.*)$ index.php [R,L,NS]

But when I tried to type in any URL it routed me to URL with full path typed -> 127.0.0.1/mvc/xxx/ routed to -> http://127.0.0.1/C:/Program%20Files/EasyPHP-12.0/apache/htdocs/mvc/index.php

Without full path (C:/Program%20Files/EasyPHP-12.0/apache/htdocs) I think, I would get what I want.

Please help how to solve this problem.

Thanks all. Kongthap.

I'm using EasyPHP on Windows XP.

Answer

Lawrence Cherone picture Lawrence Cherone · Jul 19, 2012

To expand on Jalpesh Patel's answer:

Your .htaccess will pass the url path to a router or sorts so an example URL:

http://example.com/mvc/controller/action/action2:

RewriteEngine on
RewriteBase /mvc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]

Would send to index.php?request=controller/action/action2

Then in index which hopefully will route this request to a part of the script that does something along the lines of:

/*Split the parts of the request by / */
$request = (isset($_GET['request']) ? explode('/', $_GET['request']) : null);
//but most likely $request will be passed to your url layer
$request[0] = 'controller';
$request[1] = 'action';
$request[2] = 'action2';