Kevin
Kevin

Reputation: 3623

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 site 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?

Upvotes: 1

Views: 906

Answers (3)

Gavin C
Gavin C

Reputation: 88

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

Upvotes: 1

Femi
Femi

Reputation: 64700

Try this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/([^/]+)?otherstuff=(.*)^ index.php?controller=$1&action=$2&otherstuff=$3 [L]

Upvotes: 2

simshaun
simshaun

Reputation: 21466

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>

Upvotes: 1

Related Questions