Simple MVC mod-rewrite

前端 未结 2 715
既然无缘
既然无缘 2020-12-19 11:28

I\'m not sure how to do a mod-rewrite for a modular MVC structure. What I want to happen is the URL captures:

相关标签:
2条回答
  • 2020-12-19 11:49

    You could use a PHP function to do that. Quickly, with no default value or error handling:

    list( $controller, $function, $params ) = explode( '/', $uri, 3 );
    $params = explode( '/', $uri );
    

    And in the .htaccess, redirect any non-existing query to your PHP query-handling file:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ mvc.php [L,NS]
    

    Beware to filter your input though, not to include any file, etc.

    0 讨论(0)
  • 2020-12-19 11:50

    You could also have your htaccess like this:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/(.*)/(.*) mvc.php?model=$1&view=$2&parameters=$3 [L,NS]
    

    This is just another way to do it, although personally I prefer streetpc's way.

    0 讨论(0)
提交回复
热议问题