From dcc1598e28a1114e49700ef91d593fca7dbf3f2a Mon Sep 17 00:00:00 2001 From: bengizmo Date: Mon, 28 Jul 2025 19:50:10 -0300 Subject: [PATCH] Implement comprehensive legacy URL redirect handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added pre_get_posts hook to intercept legacy page queries - Force 404 for legacy URLs to prevent WordPress from serving wrong pages - Enhanced redirect handling to catch 404s that should redirect - Moved template_redirect priority to 1 (very early) - Ensures legacy URLs like /certificate-reports/ redirect properly This follows WordPress best practices by intercepting queries early in the request lifecycle before page templates are loaded. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- includes/class-hvac-route-manager.php | 63 ++++++++++++++++++++++++++- scripts/check-page-existence.sh | 6 +++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100755 scripts/check-page-existence.sh diff --git a/includes/class-hvac-route-manager.php b/includes/class-hvac-route-manager.php index e763ebf3..f22c62f4 100644 --- a/includes/class-hvac-route-manager.php +++ b/includes/class-hvac-route-manager.php @@ -104,8 +104,11 @@ class HVAC_Route_Manager { // Register rewrite rules add_action('init', array($this, 'register_rewrite_rules'), 5); - // Handle redirects - add_action('template_redirect', array($this, 'handle_redirects'), 5); + // Handle redirects - run very early + add_action('template_redirect', array($this, 'handle_redirects'), 1); + + // Intercept page queries for legacy URLs + add_action('pre_get_posts', array($this, 'intercept_legacy_page_queries'), 1); // Register query vars add_filter('query_vars', array($this, 'register_query_vars')); @@ -216,6 +219,22 @@ class HVAC_Route_Manager { $this->perform_redirect($this->parent_redirects[$current_path]); } + // Also check if this is a 404 that should redirect + if (is_404()) { + // Check without trailing slash + $path_no_slash = rtrim($current_path, '/'); + if (isset($this->legacy_redirects[$path_no_slash])) { + $this->perform_redirect($this->legacy_redirects[$path_no_slash]); + } + + // Check with just the last segment (for pages that might exist but shouldn't) + $segments = explode('/', $current_path); + $last_segment = end($segments); + if ($last_segment && isset($this->legacy_redirects[$last_segment])) { + $this->perform_redirect($this->legacy_redirects[$last_segment]); + } + } + // Also check if this is a page that exists with a legacy slug global $post; if (is_page() && $post) { @@ -338,4 +357,44 @@ class HVAC_Route_Manager { return false; } + + /** + * Intercept page queries for legacy URLs + * + * This prevents WordPress from finding pages with legacy slugs + * and allows our redirects to work properly + * + * @param WP_Query $query The WordPress query object + * @return void + */ + public function intercept_legacy_page_queries($query) { + // Only run on the main query on the frontend + if (!$query->is_main_query() || is_admin()) { + return; + } + + // Check if this is a page query + if (!$query->is_page()) { + return; + } + + // Get the page name being queried + $pagename = $query->get('pagename'); + if (empty($pagename)) { + return; + } + + // Check if this is a legacy URL that should redirect + if (isset($this->legacy_redirects[$pagename])) { + // Prevent WordPress from finding this page + $query->set('page_id', -1); + $query->set('pagename', ''); + + // Force a 404 which will trigger our template_redirect + $query->set_404(); + + // Log for debugging + HVAC_Logger::info("Intercepted legacy page query: {$pagename}", 'Routes'); + } + } } \ No newline at end of file diff --git a/scripts/check-page-existence.sh b/scripts/check-page-existence.sh new file mode 100755 index 00000000..a0a648cb --- /dev/null +++ b/scripts/check-page-existence.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +echo "=== Checking Page Existence on Staging ===" + +# SSH into staging and check pages +ssh roodev@146.190.76.204 "cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && wp post list --post_type=page --fields=ID,post_title,post_name,post_status --format=table | grep -E 'hvac-dashboard|certificate-reports|master-dashboard|trainer-registration'" \ No newline at end of file