Merge branch 'rmitchellscott-static-redirect-index'

This commit is contained in:
Matt Walsh
2025-10-21 03:26:25 +00:00
2 changed files with 38 additions and 2 deletions

View File

@@ -10,8 +10,10 @@ server {
add_header X-Weatherstar true always;
include /etc/nginx/includes/wsqs_redirect.conf;
location / {
index redirect.html index.html index.htm;
index index.html index.htm;
try_files $uri $uri/ =404;
}

View File

@@ -12,6 +12,9 @@ url_encode() {
# build query string from WSQS_ env vars
while IFS='=' read -r key val; do
# Skip empty lines
[ -z "$key" ] && continue
# Remove WSQS_ prefix and convert underscores to hyphens
key="${key#WSQS_}"
key="${key//_/-}"
@@ -23,11 +26,16 @@ while IFS='=' read -r key val; do
QS="${key}=${encoded_val}"
fi
done << EOF
$(env | grep '^WSQS_')
$(env | grep '^WSQS_' || true)
EOF
mkdir -p /etc/nginx/includes
if [ -n "$QS" ]; then
# Escape the query string for use in JavaScript (escape backslashes and single quotes)
QS_ESCAPED=$(printf '%s' "$QS" | sed "s/\\\\/\\\\\\\\/g; s/'/\\\'/g")
# Generate redirect.html with JavaScript logic
cat > "$ROOT/redirect.html" <<EOF
<!DOCTYPE html>
<html>
@@ -35,10 +43,36 @@ if [ -n "$QS" ]; then
<meta charset="utf-8" />
<title>Redirecting</title>
<meta http-equiv="refresh" content="0;url=/index.html?$QS" />
<script>
(function() {
var wsqsParams = '$QS_ESCAPED';
var currentParams = window.location.search.substring(1);
var targetParams = currentParams || wsqsParams;
window.location.replace('/index.html?' + targetParams);
})();
</script>
</head>
<body></body>
</html>
EOF
# Generate nginx config for conditional redirects
cat > /etc/nginx/includes/wsqs_redirect.conf <<'EOF'
location = / {
if ($args = '') {
rewrite ^ /redirect.html last;
}
rewrite ^/$ /index.html?$args? redirect;
}
location = /index.html {
if ($args = '') {
rewrite ^ /redirect.html last;
}
}
EOF
else
touch /etc/nginx/includes/wsqs_redirect.conf
fi
exec nginx -g 'daemon off;'