Implement comprehensive legacy URL redirect handling
- 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 <noreply@anthropic.com>
This commit is contained in:
parent
81190ec4a0
commit
dcc1598e28
2 changed files with 67 additions and 2 deletions
|
|
@ -104,8 +104,11 @@ class HVAC_Route_Manager {
|
||||||
// Register rewrite rules
|
// Register rewrite rules
|
||||||
add_action('init', array($this, 'register_rewrite_rules'), 5);
|
add_action('init', array($this, 'register_rewrite_rules'), 5);
|
||||||
|
|
||||||
// Handle redirects
|
// Handle redirects - run very early
|
||||||
add_action('template_redirect', array($this, 'handle_redirects'), 5);
|
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
|
// Register query vars
|
||||||
add_filter('query_vars', array($this, '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]);
|
$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
|
// Also check if this is a page that exists with a legacy slug
|
||||||
global $post;
|
global $post;
|
||||||
if (is_page() && $post) {
|
if (is_page() && $post) {
|
||||||
|
|
@ -338,4 +357,44 @@ class HVAC_Route_Manager {
|
||||||
|
|
||||||
return false;
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
6
scripts/check-page-existence.sh
Executable file
6
scripts/check-page-existence.sh
Executable file
|
|
@ -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'"
|
||||||
Loading…
Reference in a new issue