How to modify the URI routing for a MVC PHP site?
I'm trying rewrite an ASP.NET MVC 2 site I built using PHP/Apache for fun.
I've built a basic MVC framework which serves requests. The request are executed through index.php?controller=&action=&otherstuff=123123
I'd like to be able to navigate my sit开发者_如何转开发e by typing in /controller/action?otherstuff=123123
The webhost supports mod_rewrite, can I set this up using a .htaccess file in the same location as the index.php script? How do I get this to work?
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/([^/]+)?otherstuff=(.*)^ index.php?controller=$1&action=$2&otherstuff=$3 [L]
I think I'd recommend that you use the Query String Append / qsa directive. This means that if a query string is not passed, your rewrite will continue to work just fine.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/([^/]+)$ index.php?controller=$1&action=$2 [qsa,last]
This way, if you decide to use more query strings later, just go ahead - it will pass them.
G
This is what I'm using. The framework parses the route.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule .* index.php/$0 [PT]
</IfModule>
精彩评论