Olivier Pons
Olivier Pons

Reputation: 15778

Apache ModRewrite regular expression

Here's what I'd like to do:

/gestion/aa/       => /gestion/aa.php
/gestion/bb/       => /gestion/bb.php
/gestion/a/b/      => /gestion/a/b.php
/gestion/a/b/c/    => /gestion/a/b/c.php

/cmd/aa/           => /cmd/aa.php
/cmd/bb/           => /cmd/bb.php
/cmd/a/b/          => /cmd/a/b.php
/cmd/a/b/c/        => /cmd/a/b/c.php

/json/aa/          => /json/aa.php
/json/bb/          => /json/bb.php
/json/a/b/         => /json/a/b.php
/json/a/b/c/       => /json/a/b/c.php

This brought me to this simple regular expression:

^/(gestion|cmd|json)/(([^/]+/)*)$ /$1/$2.php [QSA,L]

The problem is that $2 has always the / within so the results are:

/json/aa/          => /json/aa/.php
/json/bb/          => /json/bb/.php
/json/a/b/         => /json/a/b/.php
/json/a/b/c/       => /json/a/b/c/.php

Any idea how to get the right regexp?

Upvotes: 0

Views: 49

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

Use this:

^(gestion|cmd|json)(/.*)/$ /$1$2.php

Upvotes: 0

Qnan
Qnan

Reputation: 3744

^/(gestion|cmd|json)/(.+)/$ /$1/$2.php [QSA,L] should do the trick

Upvotes: 1

Related Questions