edgars
edgars

Reputation: 1068

How to match subdirectories in RewriteCond?

Having trouble with proper regex for RewriteCond

RewriteCond %{REQUEST_URI} !^/foo/

Works as expected, that is, does not apply following rewrite to all URLs that start with /foo/.

RewriteCond %{REQUEST_URI} !^/foo/bar/

On the other hand does not work as I expect. URLs that begin with /foo/bar/ are still being rewrited.

How do I enter proper regex for excluding subdirectories?

Upvotes: 7

Views: 16525

Answers (1)

Gumbo
Gumbo

Reputation: 655319

Maybe it’s the new URL of an internal redirect the rule is applied to. The L flag does that.

[…] if the RewriteRule generates an internal redirect (which frequently occurs when rewriting in a per-directory context), this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.

If you want to make sure that the initial URL path didn’t start with „/foo/bar“, check the request line (see THE_REQUEST variable) instead:

RewriteCond %{THE_REQUEST} !^[A-Z]+\ /foo/bar/
RewriteRule …

Upvotes: 9

Related Questions