steeped
steeped

Reputation: 2633

Modifying .htaccess rewrite rule for MVC application

This is the rewrite rule I currently use for my MVC application:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(/([^/]+))?$ application/controller/$1.php?method=$2&param=$4

Displays as: www.website.com/controller/method/param

The param URI is optional. How would I make method optional as well, so it will allow the user to go to www.website.com/controller?

Could I also additionally force the url to have a trailing slash?

Upvotes: 0

Views: 405

Answers (1)

Jon Lin
Jon Lin

Reputation: 143946

Try:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+)|)(?:/([^/]+)|) application/controller/$1.php?method=$2&param=$3 [L]

The (?:/([^/]+)|) are optional capture groups that allows for a "nothing" using the | symbol.

Upvotes: 2

Related Questions