The Mod_Rewrite Misunderstanding in PHP Docker Compose Stacks
When debugging a .htaccess file that refuses to rewrite URLs inside a Docker container, it is common to instinctively check the PHP module list. If mod_rewrite is missing from php -m, you might assume the Apache configuration within your container failed to load the module. However, in this specific architecture, the absence of mod_rewrite in the PHP process output is expected behavior and not the root cause of your issue.
The confusion stems from a fundamental architectural distinction between how PHP modules work versus how Apache modules operate. The command php -m lists extensions loaded into the PHP interpreter, such as mysqli, pdo, or session. It does not list Apache server modules. The mod_rewrite module is an Apache directive, not a PHP extension. Therefore, seeing it missing from the PHP output in your container confirms that PHP itself hasn't been compiled with an Apache-specific rewrite engine, which is correct because PHP runs as a FastCGI process (via FPM or via the apache2 package's internal handler), while Apache handles the HTTP parsing and rewriting logic.
To resolve the issue where your .htaccess rules are ignored, you must inspect the Apache configuration inside the container rather than the PHP module list. Based on your source material, you are running a custom apache/Dockerfile. The critical step missing from that build process is ensuring the rewrite_module is actually enabled in the Apache configuration.
In many official Apache images (like httpd:2.4), modules are loaded automatically or via .load directives in /etc/apache2/mods-enabled/. However, when building a custom image using FROM php:7.0-apache, you must explicitly ensure the module is active. The most robust way to do this is by running the Apache-specific enable command during the build:
RUN a2enmod rewrite
If your apache/Dockerfile currently only contains docker-php-ext-install mysqli, it is failing to load the rewrite module because that command targets PHP extensions, not Apache modules. You need to add a2enmod rewrite (or apt-get install libapache2-mod-php7.0 -y && a2enmod rewrite) to your Apache build script. Without this step, Apache starts without the rewrite engine enabled, rendering all .htaccess rules null and void.
Furthermore, you must verify that the Apache configuration files are being mounted correctly or if they are baked into the image. In your docker-compose.yml, both services mount ./public_html/www/:/var/www/html/. This volume mapping is correct for serving static files and PHP scripts. However, ensure that any custom .htaccess files you place in ./public_html/www/ are actually present when the container starts.
Another potential pitfall lies in your Apache virtual host configuration (apache/demo.apache.conf). You have included:
<Directory /var/www/html/>
Options All
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
The directive AllowOverride All is mandatory for .htaccess files to take effect. If this line is commented out or missing, Apache will ignore rewrite rules entirely for security reasons. Additionally, the Order allow,deny syntax is deprecated in Apache 2.4 and later, replaced by Require all granted. While your setup might still function with the old syntax depending on the specific base image version, cleaning up to modern directives is recommended to avoid unexpected behavior across different Apache versions.
Finally, ensure that you are checking the correct container when inspecting logs. Since you have a dedicated apache service and a php service, errors related to rewrite rules often appear in the Apache error log (/var/log/apache2/error.log), not the PHP CLI output. If you modify the Dockerfile to enable the module but still see no effect, restart the containers with docker-compose up -d --build to ensure the new image is used.
If you are looking for a more modern approach to handling web server logic in Laravel or similar frameworks within Docker, consider using Nginx with PHP-FPM directly, which often simplifies configuration by removing the need for Apache-specific module management. However, sticking with your current stack requires ensuring a2enmod rewrite is executed in the Apache build context and that AllowOverride All remains active in your virtual host config.