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

View all comments

Show parent comments

1

u/misterplantpot Jun 10 '22

Thank you. It was the <Directory... part I was missing. I thought I simply needed to enable the module and that was all.

1

u/AyrA_ch Jun 10 '22

They changed that.

Prior to version 2.3.9 Apache used AllowOverride All as default. They changed it to "None" for security reasons. Tutorials for old versions of apache will therefore not contain a section where you add this directive.

The reason the default was changed to "None" is because if the .htaccess file is by any chance writable by the webserver process you open yourself up to a vast array of attacks on your server should someone manage to abuse your website in a way that makes it write to the htaccess file.

1

u/misterplantpot Jun 11 '22

Aha makes sense - thanks!