Lukas Körfer
Lukas Körfer

Reputation: 14493

Apache httpd redirects from localhost not working in Docker

To publish some ontology files I want to add redirects to w3id.org, which is simply an Apache HTTP Server, that can be configured through .htaccess files in a public GitHub repository.

Since I am not familiar with the Apache server and HTTP redirects at all, I tried to get w3id running in a local Docker container. Therefore, I followed the instruction on the httpd Docker image and created a Dockerfile containing:

FROM httpd:2.4
COPY ./public-html/ /usr/local/apache2/htdocs/

Of course, I replaced ./public-html/ with the path to my local w3id repository files.

I used the commands below to build and run the container:

$ docker build -t w3id.org .
$ docker run -dit --name w3id.org -p 8080:80 w3id.org

Afterwards, I can successfully access an endpoint at localhost:8080 which provides the simple HTML landing page of the w3id project.

However, if I try to access for example localhost:8080/security, I will not get redirected in any way, in contrast to when I access w3id.org/security, which points me to web-payments.org/vocabs/security like intended. Instead, the request provides an overview about all files inside the specific directory security, which seems to be the default behaviour for all requests.

The example .htaccess file inside the security directory contains:

Header set Access-Control-Allow-Origin *
Header set Access-Control-Allow-Headers DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified$
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^$ https://web-payments.org/vocabs/security [R=302,L]
RewriteRule ^v1$ https://web-payments.org/contexts/security-v1.jsonld [R=302,L]
RewriteRule ^v2$ https://web-payments.org/contexts/security-v2.jsonld [R=302,L]

Is this caused by my Dockerfile or the way I run the container? Do I need to enable redirects in Apache? Or maybe caused by missing HTTPS in my local setup?

How can I locally setup and test external redirects in .htaccess files?

Upvotes: 0

Views: 2758

Answers (1)

DannyB
DannyB

Reputation: 14776

You might need to enable mod_rewrite and enable overriding with .htaccess.

See the below Dockerfile for a working reference:

FROM httpd:alpine

RUN sed -i '/LoadModule rewrite_module/s/^#//g' /usr/local/apache2/conf/httpd.conf && \
    sed -i 's#AllowOverride [Nn]one#AllowOverride All#' /usr/local/apache2/conf/httpd.conf

WORKDIR /usr/local/apache2/htdocs/

COPY ./public .

Then, redirecting with .htaccess can be done like this:

RewriteEngine On
Redirect /whatever http://example.com

I would suggest trying this (as is) first in the docroot, to ensure your redirects work, and then see if you want / need to move your .htaccess deeper to the security directory, and tweak further as needed.

Finally, since I am not sure if you pasted the exact docker commands you are using or just partial commands, I would suggest mounting the public directory to the container during development, this way you do not need to rebuild on every change for debug.

$ docker build -t temp .
$ docker run -i -p 3000:80 -v $PWD/public:/usr/local/apache2/htdocs/ temp

Upvotes: 4

Related Questions