Apache regex in RewriteRule, "or" syntax

I'm tired of getting the whole line before the first slash, when the URL ends with the words "word1" or "word2" and im uses this code:

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

I have no problem with a URL that has "word1" at its ends, but if the URL ends with "word2", the Apache dose does not return any value for the variable "query"

+4
source share
2 answers

You need to group the variables in your expression:

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

Without grouping, your expression means:

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

+5
source

Using:

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

All Articles