HEX
Server: nginx/1.29.3
System: Linux 11979.bigscoots-wpo.com 6.8.0-88-generic #89-Ubuntu SMP PREEMPT_DYNAMIC Sat Oct 11 01:02:46 UTC 2025 x86_64
User: nginx (1068)
PHP: 7.4.33
Disabled: exec,system,passthru,shell_exec,proc_open,proc_close,popen,show_source,cmd# Do not modify this line # 1684243876
Upload Files
File: //proc/1284358/cwd/bigscoots/wpo_redirects.sh
#!/bin/bash

# Usage: 
# ./script.sh domain.com manual add "(?i)^/source/?$" https://target.com 301
# ./script.sh domain.com manual list

domain="$1"
conf_path="/usr/local/nginx/conf/wpincludes/$domain/wpo_manual_redirects.conf"

# Ensure the include directory exists
if [ ! -d "/usr/local/nginx/conf/wpincludes/$domain" ]; then
    mkdir -p "/usr/local/nginx/conf/wpincludes/$domain"
fi

case $2 in
manual)
    # Ensure the include directive exists in the Nginx vhost configs
    for conf in "/usr/local/nginx/conf/conf.d/$domain.ssl.conf" "/usr/local/nginx/conf/conf.d/$domain.conf"; do
        if [ -f "$conf" ] && ! grep -q "wpo_manual_redirects.conf" "$conf"; then
            sed -i "/location \/ {/a \  include $conf_path;" "$conf"
        fi
    done

    touch "$conf_path"

    if [[ $3 == add || $3 == modify ]]; then
        if [[ $3 == add ]]; then
            uuid=$(uuidgen -r | sed 's/-//g')
            source="$4"
            target="$5"
            code="$6"
        else
            uuid="$4"
            source="$5"
            target="$6"
            code="$7"
        fi

        # Determine Nginx redirect type
        ngxcode="permanent;"
        [[ $code == 302 ]] && ngxcode="redirect;"

        # We wrap both source and target in double quotes. 
        # This prevents Nginx from miscounting arguments if the URL has '?' or '&'.
        rule="rewrite \"$source\" \"$target\" $ngxcode # $uuid"

        if [[ $3 == modify ]] && grep -q "$uuid" "$conf_path"; then
            # Update existing line by UUID
            sed -i "/$uuid/c $rule" "$conf_path"
        else
            # Append new rule
            echo "$rule" >> "$conf_path"
        fi

        # Syntax check and reload
        if nginx -t > /dev/null 2>&1; then
            ngxreload > /dev/null 2>&1
            echo -n "$uuid"
        else
            # Rollback: Remove the faulty line if nginx -t fails
            sed -i "/$uuid/d" "$conf_path"
            exit 1
        fi
    fi
;;

remove)
    uuid=$3
    if [[ "$uuid" == "ALL" ]]; then
        cp "$conf_path" "${conf_path}.backup.$(date +%Y%m%d-%H%M%S)"
        echo > "$conf_path"
    else
        sed -i "/$uuid/d" "$conf_path"
    fi

    if nginx -t > /dev/null 2>&1; then
        ngxreload > /dev/null 2>&1
    else
        exit 1
    fi
;;

list)
    # 1. Grab all lines once
    # 2. Use AWK to create a clean TSV (Tab Separated Values) stream
    # 3. Pipe that whole stream into ONE jq process
    grep "^rewrite" "$conf_path" | awk -F'# ' '
    {
        # Get the UUID (part after #)
        id=$2;
        
        # Clean the rewrite line
        line=$1;
        gsub(/^rewrite | permanent;| redirect;|"/, "", line);
        
        # Split into src and dst
        split(line, parts, " ");
        src=parts[1];
        dst=parts[2];
        
        # Determine type
        type = ($0 ~ /permanent;/) ? "301" : "302";
        
        # Print as TSV for jq to consume
        print src "\t" dst "\t" type "\t" id
    }' | jq -Rn --arg dom "$domain" '
      { ($dom): [
        inputs | split("\t") | {
          source: .[0],
          destination: .[1],
          type: .[2],
          uuid: .[3]
        }
      ] }'
;;
esac