Hamidreza
Hamidreza

Reputation: 1915

Apache regular expression on RewriteRule, "or" syntax

I'm tiring to get all string before first slash when an url ends with "word1" or "word2", and im using this code:

RewriteRule ^(.+)/word1|word2/?$            index.php?query=$1 [NC]

I haven't any problem with url that have "word1" on his ends, but if the url ends with "word2", Apache dose not return any value to "query" variable

Upvotes: 4

Views: 7764

Answers (2)

William Greenly
William Greenly

Reputation: 3989

Use:

RewriteRule ^(.+)/(word1|word2)/?$

Upvotes: 2

Qtax
Qtax

Reputation: 33918

You need to group the alternations in your expression:

^(.+)/(?:word1|word2)/?$

Without the grouping your expression means:

^(.+)/word1 or word2/?$

Upvotes: 5

Related Questions