Faced with the fact that .htaccess ignored the L flag in the RewriteRule , resulting in a conflict of two rules. It was too lazy to figure out what was happening, and I decided to just change the regular rule for the first rule so that it never intersects with the second one. Actually, the rules:

 Если запрашиваем /<имя файла>.js, то отдать файл /public/scripts/<имя файла>.js Если запрашиваем /lib/<имя файла>.js, то отдать файл /libs/js/<имя файла>.js 

Therefore, if the first rule does not process requests that begin with /lib/ , then the problem will disappear by itself. For example:

 RewriteRule ^(?<!lib\/)((.*)(\.js))$ public/scripts/$1 [L] RewriteRule ^lib\/(.*)(\.js)$ libs/js/$1$2 [L] 

Past Apache in general does not know such a construction, not to mention the constant errors 500, which come out in the most unexpected places. But this rule works (discovered by chance, thanks to a typo):

 RewriteRule ^lib\/(.*)(\.js)$ libs/js/$1$2 [L] RewriteRule ^(lib)?([^\/]*\.js)$ public/scripts/$2 [L] 

From the point of view of regulars known to me, the construction, at the beginning of the second rule, reads as "the beginning of a line may contain the substring lib ", but Apache clearly understands it somehow, because in this form the second rule no longer conflicts with the first. I tried to change their places, thinking that maybe the flag L could have just earned, but the order of these rules does not affect their behavior.

In connection with the above, I have a question: what is the general syntax of regular expressions that Apache uses (version 2.4.7, if that)? And where can I find complete documentation on it?

  • one
  • (?<!lib\/) is a preview block backward. The Apache documentation states that it supports PCRE regular expressions, in which the construct you specified is supported . Although you actually had to use the forward preview block, RewriteRule ^(?!lib/)(.*\.js)$ public/scripts/$1 [L] . The second example works only because [^\/]* finds 0+ characters other than / (everything with a slash will not be processed). - Wiktor Stribiżew
  • @Risto, the L flag is not ignored. If the rule worked with such a flag, then the further rules are not executed, BUT (if I'm not mistaken) if this rule has changed the request, then apache processes the .htaccess again from the very beginning. - Visman
  • one
    @Risto, in English is not strong, but the behavior is so strange. Here on the site there are many questions about this about looping forwarding. It is usually decided by setting a variable by which it is determined whether the change has already been made or not. - Visman
  • one
    Any problems with htaccess start with a careful study of the material: habrahabr.ru/company/sprinthost/blog/129560 - ReinRaus

1 answer 1

If I understood correctly

 Если запрашиваем /<имя файла>.js, то отдать файл /public/scripts/<имя файла>.js Если запрашиваем /lib/<имя файла>.js, то отдать файл /libs/js/<имя файла>.js 

request to js files goes either to the root or to the lib folder, then do so

 RewriteRule ^lib/([^/]+)\.js$ /libs/js/$1.js [L] RewriteRule ^([^/]+)\.js$ /public/scripts/$1.js [L] 
  • Only ([^/]*) , but not ([^/]) , in the same place not on one character. Correct, please, and a non-author cannot be corrected in two characters, at least 6. And so it works, thanks. I did not know that the slashes should not be screened, I was surprised. - Risto
  • one
    Then too ([^/]+) . Sclerosis :) - Visman