Apache regular expression on RewriteRule, “or” syntax

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

Solutions/Answers:

Solution 1:

You need to group the alternations in your expression:

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

Without the grouping your expression means:

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

Solution 2:

Use:

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

References

Related:  Regex to match filename at end of URL