Home > Enterprise >  confusion between two rewrite urls rules
confusion between two rewrite urls rules

Time:11-23

these two rules are confused :

RewriteRule ^health-institute-([a-zA-Z\-] )-([a-zA-Z\-] )$ search.php?city=$1&speciality=$2 [L] 
RewriteRule ^health-institute-app-([a-zA-Z\-] )$ search.php?city=$1 [L]

when I want to reach health-institute-app-mycity (2nd rule) the server consider app as a value and try to reach search.php?city=app&speciality=mycity (1st rule)

how can I say that these are two separate rules?

CodePudding user response:

Yes, because the regex ^health-institute-([a-zA-Z\-] )-([a-zA-Z\-] )$ in the first rule also matches health-institute-app-mycity.

You need to reverse these two directives so the more specific rule is first.

For example:

RewriteRule ^health-institute-app-([a-zA-Z-] )$ search.php?city=$1 [L]
RewriteRule ^health-institute-([a-zA-Z-] )-([a-zA-Z-] )$ search.php?city=$1&speciality=$2 [L] 

(No need to backslash-escape the hyphen when at the start or end of the character class.)

UPDATE: However, the regex in the (now) second rule is potentially ambiguous since the hyphen (-) is used to delimit the two values (city and speciality), but the hyphen is also included in both the character classes, so it can presumably be part of the values themselves. However, both city and speciality cannot both contain hyphens, despite the regex seemingly allowing this.

For example, how should a request for health-institute-foo-bar-baz-qux be resolved? Since the quantifier is greedy, this will currently result in search.php?city=foo-bar-baz&speciality=qux. If there is ever a hyphen in the speciality (as suggested this could be the case by the regex) it will never be matched.

  • Related