Course Progress 10%

How WordPress Internally Detects a 404 Request (Rewrite Rules, Query Vars, is_404 Flow)

इस Chapter में हम WordPress 404 handling के core internal mechanism को समझेंगे। यह सबसे important technical chapter है, क्योंकि जब तक आपको यह नहीं पता होगा कि WordPress 404 को decide कैसे करता है, तब तक कोई भी redirect rule, plugin, या fix पूरी तरह logical नहीं लगेगा।

बहुत से tutorials सीधे solution पर कूद जाते हैं, लेकिन WordPress में 404 कोई magic नहीं है। यह एक step-by-step decision flow का result होता है।

Request Lifecycle: URL से WordPress तक

जब कोई user या bot किसी URL को open करता है, तब process इस तरह शुरू होता है:

  1. Browser URL को server को request करता है
  2. Server (Apache / Nginx) उस request को WordPress entry point तक भेजता है
  3. WordPress उस URL को parse करता है
  4. WordPress decide करता है कि content exist करता है या नहीं

404 decision step 3 और 4 के बीच लिया जाता है, न कि theme या plugin level पर।

Role of Rewrite Rules in WordPress

WordPress pretty URLs को handle करने के लिए rewrite rules का use करता है।

Example URL:
/angular/directives/

Internally WordPress इसे कुछ इस तरह convert करता है:

  • post_type = post
  • name = directives
  • category = angular

यह conversion .htaccess (Apache) या server config के through होती है।

Important point:
Rewrite rules सिर्फ URL को WordPress तक पहुँचाती हैं
Rewrite rules यह decide नहीं करतीं कि content exist करता है या नहीं

Query Vars: WordPress का Internal Language

Rewrite rules के बाद WordPress कुछ internal variables set करता है, जिन्हें query vars कहा जाता है।

Common query vars:

  • p
  • name
  • post_type
  • category_name
  • paged

WordPress इन्हीं query vars के base पर database query बनाता है।

अगर query vars गलत हैं या content database में मौजूद नहीं है, तो WordPress को result नहीं मिलता।

WP_Query और Database Lookup

अब WordPress का core class WP_Query activate होता है।

Process:

  • Query vars database query में convert होते हैं
  • wp_posts और related tables search होते हैं
  • Matching post/page/taxonomy ढूँढा जाता है

अगर:

  • कोई matching row मिल जाती है → valid content
  • कोई result नहीं मिलता → potential 404

यह decision पूरी तरह database result पर based होता है।

is_404 Flag कैसे Set होता है

जब WP_Query को कोई valid result नहीं मिलता, तब WordPress internally यह करता है:

  • $wp_query->is_404 = true
  • $wp_query->is_singular = false
  • $wp_query->posts = empty

यह is_404 flag ही central truth है।

Theme, plugin, redirect logic, सब इसी flag को देखकर behave करते हैं।

Important:
404 response तभी final होता है जब is_404 true रहे
अगर बीच में कोई code इसे override कर दे, तो 404 change हो सकता है

Template Hierarchy और 404.php

is_404 true होने के बाद WordPress template hierarchy follow करता है।

Sequence:

  • 404.php
  • fallback templates

अगर 404.php file मौजूद है, तो वही render होती है।

यहाँ ध्यान देने वाली बात:
404.php सिर्फ presentation layer है
404 decision इससे पहले लिया जा चुका होता है

इसलिए सिर्फ 404.php edit करने से 404 solve नहीं होता।

Why WordPress Still Returns 404 Header

भले ही आप:

  • custom message दिखाएँ
  • redirect करें
  • content inject करें

अगर is_404 true है और header override नहीं हुआ, तो WordPress HTTP response में:

HTTP/1.1 404 Not Found

भेज सकता है।

यही reason है कि:

  • Page redirect हो रहा होता है
  • लेकिन plugin logs में 404 count बढ़ता रहता है

क्योंकि header level पर 404 पहले ही fire हो चुका होता है।

Plugin और Redirect Code कहाँ Fail होता है

बहुत से redirect plugins या functions.php codes:

  • template_redirect hook पर चलते हैं
  • wp action के बाद execute होते हैं

लेकिन:

  • is_404 flag उससे पहले set हो चुका होता है
  • monitoring plugin उस moment को log कर लेता है

इसलिए:
Redirect visible है
लेकिन internal log में 404 record हो चुका होता है

यह WordPress design limitation है, bug नहीं।

Server-Level 404 vs WordPress-Level 404

यह distinction समझना बहुत ज़रूरी है।

Server-level 404:

  • Request WordPress तक पहुँची ही नहीं
  • .htaccess या server config ने 404 भेज दिया

WordPress-level 404:

  • Request WordPress तक पहुँची
  • WP_Query failed
  • is_404 true हुआ

WordPress redirects सिर्फ WordPress-level 404 handle कर सकते हैं।

Why Global 404 Redirect Needs Precision

अगर आप blindly सभी unknown URLs को redirect कर देते हैं:

  • Valid URLs भी override हो सकते हैं
  • Admin, REST API, AJAX break हो सकता है
  • SEO signals corrupt हो सकते हैं

इसलिए correct approach यह है:

  • WordPress को content decide करने देना
  • is_404 true होने पर controlled redirect करना
  • Important system URLs को exclude करना

आपने जो solution implement किया है, वही इसी principle पर based है।

Key Takeaway of This Chapter

  • WordPress 404 core-level decision है
  • Rewrite rules सिर्फ routing करती हैं
  • WP_Query database result decide करता है
  • is_404 flag final authority है
  • Redirect दिखना और 404 log होना अलग चीजें हैं

जब यह flow clear हो जाता है, तब:

  • 404 plugins की behavior समझ में आने लगती है
  • Redirect issues logical लगते हैं
  • “Zero 404” myth automatically break हो जाता है

अगले Chapter में हम इसी flow के आधार पर एक critical truth discuss करेंगे:

Chapter 4: Why You Cannot Truly Stop 404 Generation in WordPress Core (Architectural Limitations Explained)

यह Chapter बहुत से गलत tutorials और myths को permanently clear कर देगा।