如何配置.htaccess来支持MVC?

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.

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';

a example URL: http://example.com/controller/action1/action2/action3

use this rules in your .htaccess:

<IfModule mod_rewrite.c>    
RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-l
RewriteRule ^([a-zA-Z_-]*)/?([a-zA-Z_-]*)?/?([a-zA-Z0-9_-]*)?/?([a-zA-Z0-9_-]*)$ index.php?controller=$1&action1=$2&action2=$3&action3=$4 [NC,L]

considering underline a word in the middle how the_word has been added the rule as you see _-

to retrieve these values ​​by coming so get the recover:

$controller = (isset($_GET['controller']) ? $_GET['controller'] : "IndexController";
$action1= (isset($_GET['action1']) ? $_GET['action1'] : "IndexAction";
$action2= (isset($_GET['action2']) ? $_GET['action2'] : "";
$action3= (isset($_GET['action3']) ? $_GET['action3'] : "";

After you validate if the controller class and if there is a method with the class_exists(), method_exists().

if( class_exists( $controller."Controller", false )) {
        $controller = $controller."Controller";
        $cont = new $controller(); 
        } 
        else {
        throw new Exception( "Class Controller ".$controller." not found in: "__LINE__ );           
        }

for your action: $action1

if( method_exists( $cont, $action1 )  ) {                   
$cont->$action1();
   } 
else {
 $cont->indexAction();                   
//throw new Exception( "Not found Action: <b>$action</b> in the controller: <b>$controller</b>" );           
            }