Christian Ivicevic
Christian Ivicevic

Reputation: 10885

Wrong parameters passed using mod_rewrite

Using the following rules I would to match URLs of the pattern domain.com/Controller/Action

RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?Controller=$1&Action=$2 [L]
RewriteRule ^([^/]+)/?$ index.php?Controller=$1&Action=Index [L]

When I call domain.com/Foo/Bar though the request variables are

Array ( [Controller] => index.php [Action] => Index )

What did I wrong here? Moreover how could I match no parameters at all?

Upvotes: 1

Views: 33

Answers (1)

anubhava
anubhava

Reputation: 784918

You need to add rewrite conditions to avoid rewriting for real file/directories:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ index.php?Controller=$1&Action=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?Controller=$1&Action=Index [L,QSA]

Upvotes: 1

Related Questions