Reputation: 471
My docker container apache seems to be unable to rewrite requests for .php
files. Interestingly enough, any other file type seems to work just fine.
For example with this simple .htaccess
configuration:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.php$ index.php [L]
RewriteRule ^(.*)\.html$ index.php [L]
RewriteRule ^(.*)\.txt$ index.php [L]
RewriteRule ^(.*)\.xml$ index.php [L]
Every request ending with .php
,.html
,.txt
or .xml
should be rewritten to index.php
. However all request ending with .php
are ignored and the requested file is shown, as if there was no configuration at all.
Dockerfile:
ARG APACHE_VERSION=""
FROM httpd:${APACHE_VERSION:+${APACHE_VERSION}-}alpine
RUN apk update; \
apk upgrade;
# Copy apache vhost file to proxy php requests to php-fpm container
COPY project.apache.conf /usr/local/apache2/conf/demo.apache.conf
RUN echo "Include /usr/local/apache2/conf/demo.apache.conf" \
>> /usr/local/apache2/conf/httpd.conf
.env (configuration)
APACHE_VERSION=2.4
PROJECT_ROOT=./www
compose.yml
version: "3.2"
services:
apache:
build:
context: './apache/'
args:
APACHE_VERSION: ${APACHE_VERSION}
depends_on:
- php
- mysql
networks:
- frontend
- backend
ports:
- "80:80"
volumes:
- ${PROJECT_ROOT}/:/var/www/html/
container_name: apache
Apache config:
ServerName localhost
LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so
LoadModule rewrite_module modules/mod_rewrite.so
<VirtualHost *:80>
# Proxy .php requests to port 9000 of the php-fpm container
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
DocumentRoot /var/www/html/
<Directory /var/www/html/>
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Send apache logs to stdout and stderr
CustomLog /proc/self/fd/1 common
ErrorLog /proc/self/fd/2
</VirtualHost>
I am not certainly sure if the issue lies within docker or apache so the question might need to be renamed.
Here is the full docker config that I am using if anyone wants to reproduce the issue inside their own docker: https://drive.google.com/file/d/1D_a-9d_BfomzrAjSxGNo8M3nZ1s2imU9/view?usp=sharing
It has a .bat file for automatic setup so the only thing you need to run it is a working docker.
Upvotes: 1
Views: 2534
Reputation: 785246
You have this ProxyPassMatch
line:
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
Which is proxying every *.php
requests to port 9000
of the php-fpm
container.
This directive runs before mod_rewrite
and handles all *.php
files, thus your rewrite rule in .htaccess
to rewrite all *.php
URIs is never invoked.
You can change this directive to proxy only /index.php
and let all remaining .php
requests to flow through to mod_rewrite
and .htaccess
like this.
ProxyPassMatch ^/(index\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
You can refactor your .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L,NC]
RewriteRule \.(php|html?|xml)$ index.php [NC,L]
Upvotes: 2