views:

14

answers:

1

Using Apache's RewriteEngine, how to rewrite /foo to index.php?x=foo but rewrite /foo/bar to index.php?x=foo&bar=true in one RewriteRule?

I have this now:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)? index.php?x=$1 [L,NC] # rewrites /foo to index.php?x=foo, but not /foo/bar to index.php?x=foo&bar=true

This only rewrites /foo to index.php?x=foo, but not /foo/bar to index.php?x=foo&bar=true. How can I add that to the RewriteRule?

+1  A: 

Try these rules:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)$ index.php?x=$1 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.php?x=$1&$2=true [L]

The first rule is to break if the requested URI can be mapped to an existing file or directory. Otherwise the other two rules are tested and applied if one of them matches the requested URI path.

Gumbo