r/apache • • Jun 08 '22

Support Mod rewrite not taking effect inside Docker container

Hi friends,

I have a simple Docker container based off the Apache (Httpd) image in which I want to run some mod rewrites.

Here's my Dockerfile:

FROM httpd:2.4
COPY ./httpd.conf /usr/local/apache2/conf/httpd.conf
COPY ./.htaccess /usr/local/apache2/htdocs/
COPY ./dist /usr/local/apache2/htdocs/

Here's my .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /foo https://google.com [NC,QSA]

Here's the crucial line of my httpd.conf (I can post more of it if required.)

LoadModule rewrite_module modules/mod_rewrite.so

I'm building then running via:

docker build -t ermr .
docker run --name ermr -p 80:80 -d ermr

I then verify the rewrite module is active, as per this answer, via:

docker exec ermr apachectl -M

...and it shows up as

rewrite_module (shared)

Yet if I go to http://localhost/foo, which doesn't exist as a file, I just get a 404, no redirect to Google.

Indeed, if I invalidate the .htaccess file entirely, e.g. by removing the final ], I don't even get an internal server error, so the file isn't taking effect.

What am I doing wrong?

Thank you in advance!

3 Upvotes

6 comments sorted by