पिछले Chapter में आपने production-ready global 404 redirect logic implement कर लिया है।
अब इस Chapter का focus एक level और deep है: किन situations में 404 redirect को deliberately skip करना चाहिए और क्यों यह long-term stability के लिए critical है।
Large WordPress sites में 404 redirect का गलत scope सबसे common breakage reason होता है, इसलिए यह Chapter optional नहीं बल्कि mandatory समझिए।
Why Exclusions Matter More Than Redirect Itself
Global 404 redirect लगाना technically आसान है, लेकिन असली challenge यह है कि:
- WordPress internally क्या-क्या request करता है
- Plugins background में कौन-से endpoints hit करते हैं
- Admin, API और async calls कैसे work करती हैं
अगर आपने blindly हर 404 को redirect कर दिया, तो:
- Admin features silently fail करेंगे
- REST-based plugins break होंगे
- AJAX responses HTML page बन जाएँगे
- Debug करना मुश्किल हो जाएगा
इसलिए exclusions redirect logic का core हिस्सा हैं, add-on नहीं।
Understanding WordPress Request Types
Redirect logic लिखने से पहले WordPress request types समझना ज़रूरी है।
Broadly WordPress requests चार categories में आते हैं:
- Public frontend requests
- Admin dashboard requests
- Background / async requests
- API-based programmatic requests
Global 404 redirect सिर्फ first category के लिए होता है।
Excluding Admin Requests (is_admin)
Admin area को redirect logic से बाहर रखना सबसे पहला और non-negotiable rule है।
if ( is_admin() ) {
return;
}
यह condition ensure करती है कि:
/wp-admin/safe रहे- Post editor, settings, plugins unaffected रहें
- Admin-side 404 testing accidentally redirect न हो
Important point:is_admin() users role check नहीं करता, बल्कि request context check करता है।
Excluding AJAX Requests (DOING_AJAX)
WordPress AJAX requests usually:
admin-ajax.phpके through जाती हैं- JSON या raw output expect करती हैं
अगर आप इन्हें redirect कर देंगे, तो frontend features silently टूट जाते हैं।
Correct exclusion:
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
Without this:
- Forms submit नहीं होंगे
- Load-more buttons fail करेंगे
- Background actions incomplete रहेंगे
Excluding REST API Requests (REST_REQUEST)
Modern WordPress heavily REST API पर dependent है।
Use cases:
- Gutenberg editor
- Headless setups
- Mobile apps
- SEO plugins
- Analytics integrations
REST API request को redirect करना critical mistake है।
Correct exclusion:
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return;
}
अगर यह skip नहीं किया गया:
- Editor errors आएँगे
- API clients wrong HTML response पाएँगे
- External services break होंगी
Excluding Feeds (RSS / Atom)
Feeds technically pages नहीं होतीं, लेकिन WordPress उन्हें template के through handle करता है।
Safe exclusion:
if ( is_feed() ) {
return;
}
Reason:
- Feed readers redirect handle नहीं करते
- SEO tools feed URLs crawl करते हैं
- Invalid feed = trust issue
Excluding the Redirect Target Page Itself
यह सबसे subtle लेकिन dangerous mistake है।
अगर redirect target page itself कभी 404 बनी, तो:
- Infinite redirect loop create होगा
Safe approach:
if ( is_page( 'important-update' ) ) {
return;
}
Always ensure:
- Redirect destination redirect logic से बाहर हो
Full Recommended Exclusion Structure
अब सभी exclusions को logical order में combine करें:
add_action( 'template_redirect', function () {
if ( is_admin() ) {
return;
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return;
}
if ( is_feed() ) {
return;
}
if ( is_page( 'important-update' ) ) {
return;
}
if ( is_404() ) {
wp_redirect( home_url( '/important-update/' ), 301 );
exit;
}
});
यह structure:
- Clear है
- Readable है
- Debug-friendly है
- Production-grade है
Why Order of Conditions Matters
Conditions का order random नहीं होना चाहिए।
Correct principle:
- Cheap checks first
- Global skips first
- Redirect last
Reason:
- Performance better रहती है
- Unexpected behavior avoid होता है
Common Exclusion Mistakes Seen in Real Sites
- is_404 check सबसे ऊपर लगाना
- REST exclusion भूल जाना
- Admin check remove कर देना
- Redirect target page ignore करना
- Plugin और functions.php दोनों में same logic
इन mistakes से site immediately नहीं टूटती, लेकिन long-term instability पैदा होती है।
SEO Perspective on Exclusions
Search engines:
- REST या AJAX URLs crawl नहीं करते
- Admin URLs index नहीं करते
लेकिन:
- Wrong redirects crawl budget waste करते हैं
- Soft-404 signals generate करते हैं
Proper exclusions SEO hygiene का हिस्सा हैं।
Key Takeaway of This Chapter
- Global redirect control से powerful है
- Exclusions optional नहीं, mandatory हैं
- WordPress request context समझना ज़रूरी है
- One-size-fits-all redirect dangerous होता है
अब आपके पास:
- Safe redirect logic
- Clean exclusions
- Stable production behavior
