Skip to content

Instantly share code, notes, and snippets.

@presswizards
Last active November 7, 2025 17:46
Show Gist options
  • Select an option

  • Save presswizards/cbe6ba16b81f6a025afd539d9c17720f to your computer and use it in GitHub Desktop.

Select an option

Save presswizards/cbe6ba16b81f6a025afd539d9c17720f to your computer and use it in GitHub Desktop.
enhance.com control panel check and create nginx rewrite rules after website migration if needed
#!/bin/bash
# Enhance.com API Script - Check and Create nginx Rewrite Rules
# This script checks if a rewrite rule exists for "/" to "index.php" and creates it if missing
# Pretty much required when migrating any WP site from Apache / OLS / LS server to nginx
# Run it first to check, answer N, add any No sites to the exclude list, then run it again, answer Y.
# Configuration
API_KEY="your-access-token-here"
ORG_ID="your-enhance-org-id-admin-or-reseller"
API_BASE_URL="https://your-enhance.your-domain.com/api"
# Exclude list - all domains to skip (all Enhance service sites, etc.)
EXCLUDE_DOMAINS=(
"phpmyadmin.enhance.your-domain.com"
"webmail.enhance.your-domain.com"
"enhance.your-domain.com"
"server2.your-domain.com"
"non-wp-site-domain.com"
)
# Function to make API calls
api_call() {
local method=$1
local endpoint=$2
local data=$3
if [ -n "$data" ]; then
curl -s -X "$method" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d "$data" \
"${API_BASE_URL}${endpoint}"
else
curl -s -X "$method" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
"${API_BASE_URL}${endpoint}"
fi
}
echo "========================================"
echo "Enhance Webserver Rewrite Rule Checker"
echo "========================================"
echo ""
echo "Using Organization ID: $ORG_ID"
echo ""
# Get all websites
echo "Fetching all websites..."
WEBSITES_RESPONSE=$(api_call "GET" "/orgs/$ORG_ID/websites?recursion=infinite")
# Arrays to store results
MISSING_DOMAINS=()
MISSING_DOMAIN_IDS=()
HAS_REWRITE=()
EXCLUDED=()
# Function to check if domain is in exclude list
is_excluded() {
local domain=$1
for excluded in "${EXCLUDE_DOMAINS[@]}"; do
if [ "$domain" = "$excluded" ]; then
return 0
fi
done
return 1
}
# Parse all websites
TOTAL=$(echo "$WEBSITES_RESPONSE" | jq -r '.items | length')
echo "Found $TOTAL websites. Checking rewrite rules..."
echo ""
# Loop through all websites and check for rewrite rules
while IFS='|' read -r website_id domain_id domain_name; do
if [ -z "$domain_id" ] || [ "$domain_id" = "null" ]; then
continue
fi
# Check if domain is in exclude list
if is_excluded "$domain_name"; then
echo "⊝ $domain_name - excluded (service site)"
EXCLUDED+=("$domain_name")
continue
fi
# Get webserver rewrites for this domain
REWRITES_RESPONSE=$(api_call "GET" "/v2/domains/$domain_id/webserver_rewrites")
# Check if the specific rewrite rule exists (/ -> index.php)
REWRITE_EXISTS=$(echo "$REWRITES_RESPONSE" | jq -r '.[]? | select(.path == "/" and .destinationFile == "index.php") | .path')
if [ -n "$REWRITE_EXISTS" ]; then
echo "✓ $domain_name - rewrite exists"
HAS_REWRITE+=("$domain_name")
else
echo "✗ $domain_name - rewrite MISSING"
MISSING_DOMAINS+=("$domain_name")
MISSING_DOMAIN_IDS+=("$domain_id")
fi
done < <(echo "$WEBSITES_RESPONSE" | jq -r '.items[] | "\(.id)|\(.domain.id)|\(.domain.domain)"')
echo ""
echo "========================================"
echo "Summary"
echo "========================================"
echo "Total websites: $TOTAL"
echo "Excluded (service sites): ${#EXCLUDED[@]}"
echo "With rewrite rule: ${#HAS_REWRITE[@]}"
echo "Missing rewrite rule: ${#MISSING_DOMAINS[@]}"
echo ""
# If there are missing rewrites, ask to create them
if [ ${#MISSING_DOMAINS[@]} -gt 0 ]; then
echo "Websites missing rewrite rule:"
for domain in "${MISSING_DOMAINS[@]}"; do
echo " - $domain"
done
echo ""
read -p "Do you want to create the missing rewrite rules? (y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ""
echo "Creating missing rewrite rules..."
echo ""
for i in "${!MISSING_DOMAINS[@]}"; do
domain="${MISSING_DOMAINS[$i]}"
domain_id="${MISSING_DOMAIN_IDS[$i]}"
echo "Creating rewrite for: $domain"
REWRITE_DATA='{
"path": "/",
"destinationFile": "index.php"
}'
CREATE_RESPONSE=$(api_call "PUT" "/v2/domains/$domain_id/webserver_rewrites" "$REWRITE_DATA")
# Verify by fetching rewrites again
REWRITES_RESPONSE=$(api_call "GET" "/v2/domains/$domain_id/webserver_rewrites")
REWRITE_EXISTS=$(echo "$REWRITES_RESPONSE" | jq -r '.[]? | select(.path == "/" and .destinationFile == "index.php") | .path')
if [ -n "$REWRITE_EXISTS" ]; then
echo "✓ Rewrite rule created successfully for $domain"
else
echo "✗ Failed to confirm rewrite rule for $domain"
fi
echo ""
done
echo "========================================"
echo "All missing rewrite rules have been created!"
echo "========================================"
else
echo ""
echo "Skipping creation of rewrite rules."
fi
else
echo "All websites have the rewrite rule configured! ✓"
fi
echo ""
echo "Script completed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment