Brandon Minton
Brandon Minton

Reputation: 1004

.htaccess: how to make a digit parameter optional?

In my example, this URL works with these parameters:

www.website.com/search/dogs-10-cats-5

what I want is to have one of the parameters (a digit or empty) to be optional like:

www.website.com/search/dogs--cats-3

Is this possible? Here is my current rewrite rule:

RewriteRule ^search/dogs-?([0-9]+)-cats-?([0-9]+)/? index.php?SearchResults&Dogs=$1&Cats=$2 [L]

Upvotes: 0

Views: 718

Answers (1)

KingCrunch
KingCrunch

Reputation: 131831

dogs-?([0-9]*)-cats-?([0-9]+)

instead of

dogs-?([0-9]+)-cats-?([0-9]+)

+ means "one or more", * means "zero or more"

I would also recommend to omit the ?s, because this makes every second dash optional (may be intended, but I guess not)

dogs10-cats5

Upvotes: 2

Related Questions