Search code examples
regexapache.htaccesshttp-redirectmod-alias

Redirect Match with excluding URLs doesnt work


I use a RedirectMatch rule which should exclude the following two URLs:

  • citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen
  • citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen

I use this rule with regex, but I get a 500 Internal Server Error:

RedirectMatch 301 /citycards/citycards-locations/muenchen/((?!citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen|citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen ).+)$ /citycards/citycards-locations/muenchen/$1

Any ideas why it doesn't work?


Solution

  • Your rule currently is: (broken down to multiple lines for better display/understanding):

    RedirectMatch
    301
    /citycards/citycards-locations/muenchen/((?!citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen|citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen ).+)$
    /citycards/citycards-locations/muenchen/$1
    

    Basically, your regex says that:

    • match /citycards/citycards-locations/muenchen/
    • which is not followed by either of the following

      1. citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen,
      2. citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen (it has a space after 18‌​-munchen)
    • match everything until the end of URI

    and redirect the matched URI to: /citycards/citycards-locations/muenchen/$1 which is basically the same URL that was matched against.

    I see 2 issues.

    1. If the blank space in your negative lookahead is not considered as a part of the pattern, you are essentially passing 4 arguments to RedirectMatch directive, leading to status 500 error
    2. If the pattern is getting parsed correctly, you have an infinite redirection loop.