diff --git a/nginx.conf b/nginx.conf index 9f6e9c0..78f8554 100644 --- a/nginx.conf +++ b/nginx.conf @@ -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; } diff --git a/static-env-handler.sh b/static-env-handler.sh index 0a6ddf1..181de46 100755 --- a/static-env-handler.sh +++ b/static-env-handler.sh @@ -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" < @@ -35,10 +43,36 @@ if [ -n "$QS" ]; then Redirecting + 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;'