Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nginx:1.15-alpine
FROM nginx:1.25-alpine

COPY start.sh /usr/local/bin/

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Possible redirect targets include domains (`mydomain.net`), paths (`mydomain.net
**Example:** `$ docker run --rm -d -e REDIRECT_TARGET=mydomain.net -p 80:80 morbz/docker-web-redirect`

### Paths are retained ###
The URL path and GET parameters are retained. That means that a request to `http://myolddomain.net/index.php?page=2` will be redirected to `http://mydomain.net/index.php?page=2` when `REDIRECT_TARGET=mydomain.net` is set.
The URL path and GET parameters are retained by default. That means that a request to `http://myolddomain.net/index.php?page=2` will be redirected to `http://mydomain.net/index.php?page=2` when `REDIRECT_TARGET=mydomain.net` is set. If you do not want to retain the path and GET parameters, set the environment variable `RETAIN_PATH` to `false`.

### Permanent redirects ###
Redirects are, by default, permanent (HTTP status code 301). That means browsers will cache the redirect and will go directly to the new site on further requests. Also search engines will recognize the new domain and change their URLs. This means this image is not suitable for temporary redirects e.g. for site maintenance.
Expand Down
26 changes: 16 additions & 10 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ else
if ! [[ $REDIRECT_TARGET =~ ^https?:// ]]; then
REDIRECT_TARGET="http://$REDIRECT_TARGET"
fi

# Add trailing slash
if [[ ${REDIRECT_TARGET:length-1:1} != "/" ]]; then
REDIRECT_TARGET="$REDIRECT_TARGET/"
fi
fi

# Default to 80
Expand All @@ -25,13 +20,24 @@ if [ ! -z "$PORT" ]; then
LISTEN="$PORT"
fi

cat <<EOF > /etc/nginx/conf.d/default.conf
server {
listen ${LISTEN};
: ${RETAIN_PATH:='true'}
if [ "$RETAIN_PATH" = "true" ]; then
cat <<EOF > /etc/nginx/conf.d/default.conf
server {
listen ${LISTEN};

rewrite ^(.*)\$ ${REDIRECT_TARGET}\$1 ${REDIRECT_TYPE};
}
EOF
else
cat <<EOF > /etc/nginx/conf.d/default.conf
server {
listen ${LISTEN};

rewrite ^/(.*)\$ ${REDIRECT_TARGET}\$1 ${REDIRECT_TYPE};
}
rewrite ^(.*)\$ ${REDIRECT_TARGET} ${REDIRECT_TYPE};
}
EOF
fi


echo "Listening to $LISTEN, Redirecting HTTP requests to ${REDIRECT_TARGET}..."
Expand Down