अब तक आपने theory, SEO impact और correct strategy पूरी तरह समझ ली है।
इस Chapter में हम उसी strategy को actual WordPress code में implement करेंगे — बिना plugin के, बिना dangerous .htaccess hacks के और बिना valid content को break किए।
यह method:
- Production-safe है
- Large websites के लिए suitable है
- WordPress core flow को respect करती है
- और वही approach है जो आपने अपनी site पर successfully apply की है
Why functions.php Is the Right Place
Global 404 redirect के लिए functions.php सबसे सही जगह क्यों है, यह समझना ज़रूरी है।
- Code WordPress lifecycle के अंदर execute होता है
- WordPress already content detect कर चुका होता है
- is_404 flag reliable होता है
- Admin और system routes safely exclude किए जा सकते हैं
इसके विपरीत:
.htaccessWordPress context नहीं जानता- Server-level rules blind होते हैं
इसलिए production sites के लिए functions.php approach preferred है।
Correct Hook to Use: template_redirect
WordPress में redirect लगाने के लिए सबसे safe hook है:
template_redirect
क्योंकि:
- WP_Query execute हो चुका होता है
- is_404 decision final होता है
- Headers अभी send नहीं हुए होते
यही perfect timing है।
Basic Global 404 Redirect Code
नीचे minimal लेकिन safe code है, जो:
- Only real 404 requests को redirect करता है
- Valid posts/pages को touch नहीं करता
add_action( 'template_redirect', function () {
if ( is_404() ) {
wp_redirect( home_url( '/important-update/' ), 301 );
exit;
}
});
यह code:
- WordPress decision को respect करता है
- User को clear destination देता है
लेकिन production site के लिए यह अभी incomplete है।
Why This Basic Code Is Not Enough
अगर आप सिर्फ ऊपर वाला code use करेंगे, तो:
- wp-admin requests affected हो सकते हैं
- REST API calls redirect हो सकती हैं
- AJAX responses break हो सकते हैं
- Feeds और system endpoints interfere हो सकते हैं
इसलिए proper exclusions mandatory हैं।
Production-Ready Global 404 Redirect Code
नीचे complete और safe version दिया गया है:
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_404() ) {
wp_redirect( home_url( '/important-update/' ), 301 );
exit;
}
});
यह code सुनिश्चित करता है कि:
- Admin panel safe रहे
- Plugins और APIs unaffected रहें
- सिर्फ real public 404 requests handle हों
Why 301 Redirect Is Used Here
301 का मतलब है:
- Permanent redirect
इस scenario में यह correct है क्योंकि:
- Missing URLs permanently removed हैं
- You want search engines to update memory
लेकिन ध्यान रखें:
- हर case में 301 mandatory नहीं होता
- Future experiments के लिए 302 भी acceptable हो सकता है
Why This Code Does Not Break Valid Content
इसका सबसे बड़ा advantage यही है।
Flow:
- WordPress पहले content detect करता है
- Valid content is_404 नहीं बनता
- Code execute ही नहीं होता
इसलिए:
- Published posts safe रहते हैं
- Custom structures unaffected रहती हैं
- SEO intact रहता है
About 404 Logs and Monitoring Plugins
इस code के साथ:
- Redirect visible होता है
- Proper header send होता है
- Monitoring plugins typically stop logging
लेकिन याद रखें:
- Some plugins early-stage detection log कर सकते हैं
- That does not mean SEO problem
यह behavior Chapter 5 में explain किया जा चुका है।
Where Exactly to Place This Code
यह code:
functions.phpमें add करें- Best practice: child theme में
Placement:
- File के end में add करना safe है
- PHP closing tag के बाद नहीं
Common Mistakes to Avoid
- Multiple redirect codes add करना
- Same logic plugin और functions.php दोनों में रखना
- Redirect page itself को condition में include करना
- Admin exclusions remove करना
Simple और controlled code हमेशा better होता है।
Key Takeaway of This Chapter
- functions.php is safest place
- template_redirect is correct hook
- is_404 is final authority
- Exclusions are mandatory
- Global redirect possible है, but controlled
अब आपके पास:
- Theory
- Strategy
- Production-ready code
तीनों मौजूद हैं।
अगले Chapter में हम इसी code को और refine करेंगे:
Chapter 10: Excluding Important Pages, Admin, REST API, and AJAX from 404 Redirect Logic
यह Chapter advanced exclusions और edge cases cover करेगा, जो large WordPress sites में अक्सर सामने आते हैं।
