1

httpd Apache server -> mod_rewrite -> backreferences

The following picture is about understanding the reference to parentheses ([Flags] are omitted).

mod_rewrite backreferences

If you use parentheses, backlinks are made to what is in parentheses and then you can use it somewhere in the conditions using %1 and $1.

The origin of the image is in the unofficial documentation for httpd Apache 2.2
https://publib.boulder.ibm.com/httpserv/manual70/rewrite/rewrite_intro.html#InternalBackRefs

There is no such picture in official httpd Apache 2.4 documentation
https://httpd.apache.org/docs/2.4/rewrite/intro.html#InternalBackRefs

The essence of this post is just to verify that I understood it correctly and whether it works in httpd Apache 2.4.

Would anyone know?

Ogi22
  • 11
  • 2

1 Answers1

1

This is a fundamental principle of how mod_rewrite works, whether you are using Apache 1.3, 2.2 or 2.4. So, yes, it does work the same in Apache 2.4.

(Although the diagram you posted, based off an IBM "copy"(?), not Apache's "official" document is arguably ambiguous as it's not clear in what order the RewriteCond directives are processed (blue arrows). Top-down or bottom-up? EDIT: Although you've clarified that this represents backreferences only; not the order of processing.)

The origin of the image is in the documentation for httpd Apache 2.2
https://publib.boulder.ibm.com/httpserv/manual70/rewrite/rewrite_intro.html#InternalBackRefs

As noted above, that's not the "official" documentation for Apache 2.2. The official docs for 2.2 and Apache 2.4 contain the same diagrams. These diagrams and the accompanying text describe the same process as outlined above.


The diagram from the IBM docs (which looks like an early "copy" of the Apache docs and is very much incomplete) contains the following diagram, which your diagram appears to be based:

IBM diagram

That blue line (that goes up from the RewriteRule pattern) is ambiguous IMO - is it going to the bottom condition first or the top? I would modify that diagram to be a bit more clear as to the order of processing. For example:

IBM diagram (revised)

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • >>>01. In my diagram, it is not about the process of rewriting but about the use of backlinks %1 and $1. >>>02. As for the rewriting process, I have a second diagram. To maintain clarity, I have drawn each logic separately. Putting two such complicated matters on one diagram was wrong with me. >>>03. Thank you for notifying me of the official Apache 2.2 documentation. I don't know why I thought the documentation I mentioned (IBM) was official. (my fault). It seems to be a fork in which various useful information is added. – Ogi22 Feb 28 '20 at 22:55
  • About the process of rewriting, (See, both links) >01< https://httpd.apache.org/docs/2.2/rewrite/tech.html#InternalRuleset >02< https://httpd.apache.org/docs/2.4/rewrite/tech.html#InternalRuleset – Ogi22 Feb 28 '20 at 23:05
  • 01. OK, fair enough. Thanks for clarifying. I suppose I tend not to differentiate it so much so was looking at it from both sides. 03. Actually, those IBM docs, seem to be more closely related to the [Apache 2.0 docs](https://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule) - perhaps an early version of 2.2(?), since it does say 2.2 at the top (although I noticed it did not mention the `DPI` flag, which was introduced later in 2.2.12). – MrWhite Feb 29 '20 at 00:46
  • An additional (important) consideration with `%n` backreferences, is that they derive from the "last _matched_ CondPattern", which is not necessarily the _previous_ CondPattern, as depicted in the diagram. (Although this isn't something that should/could be represented in the diagram.) – MrWhite Feb 29 '20 at 00:47
  • If I understand correctly, %N comes only from the last CondPattern. The same is apparent from the picture from documentation IBM Apache. Do not forget: If CondPatern is not met, the entire RewriteRule is not executed. See https://httpd.apache.org/docs/2.2/rewrite/tech.html#InternalRuleset or https://httpd.apache.org/docs/2.4/rewrite/tech.html#InternalRuleset – Ogi22 Feb 29 '20 at 13:09
  • "%N comes only from the last CondPattern" - Not necessarily, as mentioned above, and in [the docs](https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule), it comes from the "last _matched_ RewriteCond pattern". Conditions can be skipped entirely if they are part of an "OR" block and negated patterns are non-capturing (they don't "match" anything). In both cases, captured patterns from the last _matched_ CondPattern are essentially _passed through_. – MrWhite Mar 03 '20 at 08:37
  • If this answered your question then please mark the answer as "accepted" to help other readers and to remove the question from the unanswered question queue. Once you have 15 rep you can also upvote answers that you find helpful. Thanks, much appreciated. :) – MrWhite Mar 03 '20 at 08:41