Eric
Eric

Reputation: 10658

Mod rewrite regex

I'm trying to rewrite any url like doc/name_of_doc-doc to doc/name_of_doc and pass it to php

RewriteRule ^([0-9a-zA-Z\-\_]+)$ /doc/index.php?doc=$1

this works fine

I'm trying to deal with the fact that name_of_doc-doc could have a dot as well like name_of.doc-doc

If i go

RewriteRule ^([0-9a-zA-Z\-\_\.]+)$ /doc/index.php?doc=$1

It has to have - and _ and . How to have one OR another ? Like a dot or a dash ? What if i don't have digits ?

Thanks

Upvotes: 0

Views: 51

Answers (2)

lorenzo-s
lorenzo-s

Reputation: 17010

Why you need it? Anyway, the simplest way is to duplicate rules:

RewriteRule ^([0-9a-zA-Z\.]+)$ /doc/index.php?doc=$1
RewriteRule ^([0-9a-zA-Z\_]+)$ /doc/index.php?doc=$1

Or, one rule but harder to read:

RewriteRule ^(([0-9a-zA-Z\.]+)|([0-9a-zA-Z\_]+))$ /doc/index.php?doc=$1

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

I'm surprised your regex works at all. By the look of it it should only work if the filename is exactly one character long... Add a + after the ] to fix that.

That might fix your other bug too, since you're doing it fine.

Side-note, use [0-9a-zA-Z._-] so you don't have to escape anything (_ has no special meaning, . loses its meaning in a character class, and - has no meaning if it's the first or last character of a class)

Upvotes: 1

Related Questions