Rich Bradshaw
Rich Bradshaw

Reputation: 72975

A regex to match anything that doesn't start with a certain string

I'm writing a mod_rewrite rule and would like a hand making it do what I want.

Basically I want the following cases to work:

request | Redirect to
---------------------
cheese  | /?page=cheese
abc123  | /?page=abc123
abc/def | /?page=abc/def
a/b/c/d | /?page=a/b/c/d

(Any alphanumeric plus . - / and _ redirected)

With two special cases:

request | Redirect to
------------------------
admin   | don't redirect
media   | don't redirect

With these two I don't want any sub directories etc to redirect either.

I've got as far as:

RewriteRule ^([A-Za-z0-9-_]+)/$ /index.php?page=$1 [L]

But this only satisfies the top two tests.

Any ideas?

Upvotes: 2

Views: 235

Answers (2)

Lauri Lehtinen
Lauri Lehtinen

Reputation: 10867

I would create a separate earlier rule (as Kobi also suggested) and use the RewriteRule dash directive to prevent substitution.

A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.

^index.php - [L]
^(admin|media) - [L]
^([A-Za-z0-9-_/\.]+)$ /?page=$1 [L]

(I wasn't able to test this where I'm at right now, so you might need to edit this slightly - sorry .. but the idea should be clear)

Tested on Apache 2.2


EDIT: Was able to try this out now - needed to add the [L] to make it work.
EDIT2: Realized I didn't answer the full question, added rules for the rest of the stuff.

Upvotes: 2

Kobi
Kobi

Reputation: 138067

If your version supports it, you can you use a negative lookahead:

^(?!(?:admin|media)$)([A-Za-z0-9-_]+)/$

Now, I'm no sys admin, but you can probably have an earlier rule to match admin and media, which is probably an easier idea.

Upvotes: 1

Related Questions