Search code examples
apache.htaccessmod-rewrite

Htaccess RewriteRule for different urls to create similar link


i got problem when using those rules:

RewriteRule ^([a-z-]+)/([a-z-]+)-([0-9]+)$ /all.php?name=$1&m=$2&y=$3 [L,QSA,NC]

RewriteRule ^([a-z-]+)/year-([0-9]+)$ /year.php?name=$1&y=$2 [L,QSA,NC]

the second rule not working

when i try: /james/year-2023 it will work based on the first rule.

how can i make /james/january-2023 work with first rule and /james/year-2023 work with second rules?

Thank you!


Solution

  • Because the regex in the first rule is more general and matches URLs you are expecting to be matched by the second rule.

    You just need to reverse the two rules so the more specific rule is first:

    RewriteRule ^([a-z-]+)/year-([0-9]+)$ /year.php?name=$1&y=$2 [L,QSA,NC]
    
    RewriteRule ^([a-z-]+)/([a-z-]+)-([0-9]+)$ /all.php?name=$1&m=$2&y=$3 [L,QSA,NC]