views:

41

answers:

1

I want to write a rule that redirects all URLs of a certain pattern to a PHP file. Exception: the control/ directory where the CMS resides.

Why does

RewriteCond  %{REQUEST_URI} ^/([^control]+)/([^/]+)$   
RewriteRule .* /pages/index.php?language=%1&page=%2&%{QUERY_STRING} [L]

not work for

domain.com/deutsch/start

(it throws a 404), while

RewriteCond  %{REQUEST_URI} ^/([^control]+)/([^/]+)/debug$   
RewriteRule .* /pages/index.php?language=%1&page=%2&page_debug=yes&%{QUERY_STRING} [L]

works for

domain.com/deutsch/start/debug

?

+1  A: 

You need to use two conditions:

RewriteCond %{REQUEST_URI} !^/control/
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)$
RewriteRule .* /pages/index.php?language=%1&page=%2&%{QUERY_STRING} [L]

But you should use your pattern directly in the RewriteRule directive if possible:

RewriteCond $1 !=control
RewriteRule ^([^/]+)/([^/]+)$ /pages/index.php?language=$1&page=$2&%{QUERY_STRING} [L]

Another hint is to use the QSA flag instead of appending the query explicitly:

RewriteCond $1 !=control
RewriteRule ^([^/]+)/([^/]+)$ /pages/index.php?language=$1&page=$2 [L,QSA]
Gumbo
Thanks Gumbo, that worked. I had it worked out based on Aif's comment above but this is a great full-fledged answer for future generations.
Pekka
Are the two latter ways preferable from a performance perspective?
Pekka
@Pekka Gaier: The latter skip the test for `.*` to match.
Gumbo