Archived page · captured 2022-11-27

An excursion in mod_rewrite

Exploring how Apache's mod_rewrite module can implement Turing-complete string rewriting systems like Tag systems and cellular automata.

Turing completeness of mod_rewrite

While setting up a blosxom blog's Apache virtual host to rewrite URLs to pass them by the blosxom CGI script, the author accidentally created a redirection loop with Apache's mod_rewrite module. This led to the realization that mod_rewrite might be Turing complete.

The author determined that Apache's mod_rewrite is a bona fide string rewriting system capable of implementing both Tag systems and cellular automata, both of which can be Turing complete. Since mod_rewrite can implement any tag system or cellular automaton, it is therefore Turing complete.

Implementation framework

A proof-of-concept implementation of a rewrite system framework in mod_rewrite is provided. The framework includes rules to bootstrap rewriting with an interpreter string, add termination markers, and loop through substitutions until completion.

Apache configuration example

DirectiveValuePurpose
RewriteEngineonEnable rewriting
RewriteLogLevel3Set log level for debugging
RewriteLog/var/log/apache2/rewrite/rewrite.logLog file location

Rewrite rules structure

The rewrite rules follow a pattern where each rule matches a string with a comma prefix, removes the comma, and applies transformations. The general format is ,xyz -> xy'z, where y is changed to y' while x and z remain unchanged.

Limitations and considerations

Apache by default limits the number of internal recursions to 10, which may be reached with serious use of this system. The limit can be increased as needed. The example includes a rule that bootstraps the interpreter string by prefixing it with '-' if the input doesn't already start with one.