RewriteRule - greedy

I searched for an answer for several hours, so sorry if he was asked several times, I skipped it.

Basically I want to do correspondence in order to ignore the first directory. This first dir on the path will be different, so I thought I could use a regex. But my regex matches the file name completely:

RewriteRule ^([az]+)?/(.+)$ $2 [L] 

it works if I am one level deep:

 http://test.domain.com/one/index.php 

I get the actual root index page. This is what I want. but if I went deeper:

 http://test.domain.com/one/two/anotherfile.php 

I get a message that /anotherfile.php was not found because it is looking for a root in it. So it seems that my regex does not stop after the last [az]. I appreciate any help.

This is Apache2, if that matters at all.

0
source share
2 answers

The rewriting engine repeats all the rules until the URI is the same before and after the iteration according to the rules. Given the rule, RewriteRule ^([az]+)?/(.+)$ $2 [L] And entering: http://test.domain.com/one/index.php , this is what happens:

  • The leading slash (prefix) is removed from the URI: one/index.php
  • The rule applies, the URI matches ^([az]+)?/(.+)$
  • URI is rewritten to /index.php
  • Internal redirection, the new URI ( /index.php ) does not match the old URI ( /one/index.php ) and is triggered through rewrite rules
  • leading slash is removed
  • The rule applies, the URI does not match ^([az]+)?/(.+)$
  • URI is not overwritten. the new URI ( /index.php ) matches the old URI before starting through the rewrite mechanism ( /index.php ), rewriting stops

Resulting URI /index.php

But with input: http://test.domain.com/one/two/anotherfile.php

  • A leading slash (prefix) is removed from the URI: one/two/anotherfile.php
  • The rule applies, the URI matches ^([az]+)?/(.+)$
  • URI is rewritten in /two/anotherfile.php
  • Internal redirection, the new URI ( /two/anotherfile.php ) does not match the old URI ( /one/two/anotherfile.php ) and is triggered through rewrite rules
  • leading slash is removed ( two/anotherfile.php )
  • The rule applies, the URI matches ^([az]+)?/(.+)$
  • URI is rewritten to /anotherfile.php
  • Internal redirection, the new URI ( /anotherfile.php ) does not match the old URI ( /two/anotherfile.php ) and is triggered through rewrite rules
  • leading slash is removed ( anotherfile.php )
  • The rule applies, the URI does not match ^([az]+)?/(.+)$
  • URI is not overwritten. the new URI ( /anotherfile.php ) matches the old URI before starting through the rewrite mechanism ( /anotherfile.php ), rewriting stops

Resulting URI - /anotherfile.php

[L] does not stop the resulting URI from returning through the rewrite mechanism, it just stops the rewrite process for the current iteration. You need to change the regex or add some kind of rewriting condition. One possibility is to add RewriteRule ^[a-z+]/([az]+)?/(.+)$ $2 [L] to what you have, so the two deep directories are processed separately.

+1
source

Hmmm ...

Throw some thoughts:

  • I thought there might be a recursive RewriteRule -ing ... but I'm not sure, since you introduced the "last" rule in it. However, this is the only logical thing that, I think, could explain what is happening.

    • It is different from your Regex not stopping - it will be Apache, lasting long after we told him to refuse.
  • If Apache does not stop, I'm not sure what you have. Perhaps RewriteCond , DPI (DPI drops PATH_INFO ) by analyzing THE_REQUEST or more?

I wish I could help anymore. Hope you worked on this last year.

0
source

Source: https://habr.com/ru/post/886950/


All Articles