Multiple WordPress Blog 410 Redirect Script

Asked AI for this script to create an MU plugin that I could install with WP-CLI to redirect all 404’s to 410 – Page permanently gone.
While it might seem odd to those who redirect to the homepage, I had thousands of missing pages across several hundred blogs that had been removed for various reasons. And GSC was showing all these 404’s as failed. Hence the plugin.
And yes, I could of used Apache .htaccess, but this was easier

I ran this as /root in its own folder

Command 1 — Set the folder name first

Rename wp-404-to-410 to whatever you want before running the rest:

SCRIPT_DIR="/root/wp-404-to-410"
mkdir -p "$SCRIPT_DIR"
cd "$SCRIPT_DIR"
pwd

Command 2 — Create the MU plugin file inside that folder

cat > "$SCRIPT_DIR/force-404-to-410.php" <<'PHP'
<?php
/**
 * Plugin Name: Force 404s to 410 Gone
 * Description: Converts all WordPress 404 responses into HTTP 410 Gone responses.
 */

add_action('template_redirect', function () {
    if (is_404()) {
        status_header(410);
        nocache_headers();

        get_header();

        echo '<main style="max-width:760px;margin:40px auto;padding:20px;">';
        echo '<h1>410 Gone</h1>';
        echo '<p>This page has been permanently removed.</p>';
        echo '</main>';

        get_footer();

        exit;
    }
}, 1);
PHP

Command 3 — Create the installer script in the same folder

This installs the MU plugin across the WordPress installs.

cat > "$SCRIPT_DIR/install-404-to-410.sh" <<'BASH'
#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PLUGIN_SOURCE="$SCRIPT_DIR/force-404-to-410.php"
LOG_FILE="$SCRIPT_DIR/install-404-to-410.log"

ACCOUNTS=(
  /home/account1
  /home/account2
  /home/account3
  /home/account4
)

echo "Started: $(date)" | tee "$LOG_FILE"
echo "Using plugin source: $PLUGIN_SOURCE" | tee -a "$LOG_FILE"
echo | tee -a "$LOG_FILE"

if [ ! -f "$PLUGIN_SOURCE" ]; then
  echo "ERROR: Plugin source not found: $PLUGIN_SOURCE" | tee -a "$LOG_FILE"
  exit 1
fi

find "${ACCOUNTS[@]}" \
  -maxdepth 4 \
  -type f \
  -name wp-config.php \
  -printf '%h\n' | sort -u | while read -r WP_DIR; do

    echo "Installing on: $WP_DIR" | tee -a "$LOG_FILE"

    mkdir -p "$WP_DIR/wp-content/mu-plugins"

    cp "$PLUGIN_SOURCE" "$WP_DIR/wp-content/mu-plugins/force-404-to-410.php"

    chown --reference="$WP_DIR/wp-config.php" "$WP_DIR/wp-content/mu-plugins/force-404-to-410.php" 2>/dev/null
    chmod 644 "$WP_DIR/wp-content/mu-plugins/force-404-to-410.php"

done

echo | tee -a "$LOG_FILE"
echo "Finished: $(date)" | tee -a "$LOG_FILE"
BASH

chmod +x "$SCRIPT_DIR/install-404-to-410.sh"

Because the script uses:

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

you can rename the folder later and it should still find the plugin file correctly, as long as the plugin file stays in the same folder as the installer script.

Command 4 — Run the installer

cd "$SCRIPT_DIR"
./install-404-to-410.sh

Log file will be saved here:

/root/wp-404-to-410/install-404-to-410.log

or whatever folder name you chose.
To confirm how many installs it touched:

grep -c "Installing on:" "$SCRIPT_DIR/install-404-to-410.log

Command 5 — Test one site

Replace the domain:

curl -I https://example.com/this-page-should-not-exist-12345

You want to see:

HTTP/2 410

or:

HTTP/1.1 410 Gone

Optional removal script

Create this only if you want an easy rollback

cat > "$SCRIPT_DIR/remove-404-to-410.sh" <<'BASH'
#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_FILE="$SCRIPT_DIR/remove-404-to-410.log"

ACCOUNTS=(
  /home/account1
  /home/account2
  /home/account3
  /home/account4
)

echo "Started removal: $(date)" | tee "$LOG_FILE"
echo | tee -a "$LOG_FILE"

find "${ACCOUNTS[@]}" \
  -maxdepth 4 \
  -type f \
  -name wp-config.php \
  -printf '%h\n' | sort -u | while read -r WP_DIR; do

    TARGET="$WP_DIR/wp-content/mu-plugins/force-404-to-410.php"

    if [ -f "$TARGET" ]; then
      echo "Removing from: $WP_DIR" | tee -a "$LOG_FILE"
      rm -f "$TARGET"
    else
      echo "Not found on: $WP_DIR" | tee -a "$LOG_FILE"
    fi

done

echo | tee -a "$LOG_FILE"
echo "Finished removal: $(date)" | tee -a "$LOG_FILE"
BASH

chmod +x "$SCRIPT_DIR/remove-404-to-410.sh"

Run rollback with:

cd "$SCRIPT_DIR"
./remove-404-to-410.sh

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *