seggaeman
seggaeman

Reputation: 191

Apache 2 RewriteRule getting wrong capture group

I'm getting trouble with capture groups in a rewrite rule. Here is a sample:

RewriteRule ^([a-z]+)\.php$ test.php?captureGroup=$1

The capture group I'm getting is "test" instead of what I typed in the address bar. Strangely this works fine:

RewriteRule ^([0-9]+)\.php$ test.php?captureGroup=$1

i.e a url like "88.php" gives me "88" as capture group. What could be the problem?

Upvotes: 1

Views: 66

Answers (1)

anubhava
anubhava

Reputation: 786291

It is because your target URI (test.php) is also matching [a-z]+.php pattern and rule is executing twice.

You can use this to prevent that:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z00-9]+)\.php$ test.php?captureGroup=$1

Upvotes: 2

Related Questions