Zerium
Zerium

Reputation: 17333

How to only allow certain words in .htaccess regex?

For example, I can write the following .htaccess rule at the base directory of my domain (/):

RewriteRule ^something/[bd]on$ foo.php [L]

Which would match the following URLs, providing my domain is www.example.com:

http://www.example.com/something/bon
http://www.example.com/something/don

right? But how would I put words in there to match? For example, apple and cheese, so that it only matches:

http://www.example.com/something/apple
http://www.example.com/something/cheese

Note that I have around 40 different possibilities after something/, so please provide me with an .htaccess rule which can accommodate that.

Upvotes: 0

Views: 1118

Answers (1)

inhan
inhan

Reputation: 7470

40 words sounds long but AFAIK, the only way to make a Regex pattern match a set of options is to explicitly write them in parantheses, separated by the pipe | character.

So I think RewriteRule ^something/(word1|word2|word3)$ foo.php [L] should work for you.

EDIT:

I forgot to mention. To shorten the list you can use character groups/ranges and conditions for the words (such as colou?r or gr[ae]y) but that will make it look confusing later when you need to edit it.

Upvotes: 1

Related Questions