How to match subdirectories in RewriteCond?

15,760

Maybe it’s the new URL of an internal redirect the rule is applied to. The L flag does that.

[…] if the RewriteRule generates an internal redirect (which frequently occurs when rewriting in a per-directory context), this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.

If you want to make sure that the initial URL path didn’t start with „/foo/bar“, check the request line (see THE_REQUEST variable) instead:

RewriteCond %{THE_REQUEST} !^[A-Z]+\ /foo/bar/
RewriteRule …
Share:
15,760
edgars
Author by

edgars

Updated on June 17, 2022

Comments

  • edgars
    edgars over 1 year

    Having trouble with proper regex for RewriteCond

    RewriteCond %{REQUEST_URI} !^/foo/
    

    Works as expected, that is, does not apply following rewrite to all URLs that start with /foo/.

    RewriteCond %{REQUEST_URI} !^/foo/bar/
    

    On the other hand does not work as I expect. URLs that begin with /foo/bar/ are still being rewrited.

    How do I enter proper regex for excluding subdirectories?

  • Luca Reghellin
    Luca Reghellin about 11 years
    @Gumbo: could you please exactly explain the regexp you used in your suggestion? It works for me too, and I guess it matches the first GET, but can't understand the subsequent '/' and the reason why there's no domain before '/foo/bar/'
  • Gumbo
    Gumbo about 11 years
    @Stratboy I’ve hoped the linked pages explain everything. Well, the pattern ^[A-Z]+\ /foo/bar/ checks the HTTP request line as seen by the server’s HTTP parser and the condition is only true when the pattern is not matched. Maybe it’s the \ that distracts you but that is still part of the pattern.
  • ESP32
    ESP32 almost 8 years
    wow - no idea why %{REQUEST_URI} fails for me - however your solution is working. Thank you!