← Back to Apache Course | Chapter 7: Advanced | Lesson 3 of 4

URL Rewriting कैसे करें

यह lesson mod_rewrite's syntax से ज़ूम आउट करके उन practical patterns पर आता है जिनके लिए URL rewriting असल sites में actually इस्तेमाल होता है -- clean URLs, site restructure के बाद redirects, और canonical addresses enforce करना।

एक Framework Front Controller के लिए Clean URLs

ज़्यादातर modern frameworks (Laravel, Symfony, proxy से serve होने पर Django) हर request को एक single entry file से route करते हैं। एक rewrite rule किसी real file या directory से match न होने वाली किसी भी request को उस entry point पर भेज देता है: RewriteCond से %{REQUEST_FILENAME} एक existing file या directory नहीं है यह check करना, फिर बाकी सब कुछ को index.php (या equivalent) पर rewrite करना ताकि framework का अपना router वहाँ से आगे ले ले।

एक पुरानी Site Structure को Redirect करना

site redesign के बाद, पुराने URLs को अक्सर काम करते रहना चाहिए -- यह एक SEO और user-experience necessity है, optional niceity नहीं। Individual Redirect directives मुट्ठी भर one-off moved pages handle करते हैं; capture groups वाला एक RewriteRule एक पूरे pattern को एक साथ handle करता है, जैसे हर पुराने /articles/123 URL को एक lookup से एक नए /blog/123-post-title scheme पर map करना, या एक simpler pattern-based transformation।

एक Canonical URL Enforce करना

Search engines example.com, www.example.com, और यहाँ तक कि HTTP और HTTPS दोनों से reachable same page को अलग, competing URLs मानते हैं जब तक आप उन्हें अन्यथा न बताएं। एक RewriteCond/RewriteRule pair जो किसी भी non-www request को www. पर redirect करता है (या उल्टा -- एक चुनें और consistent रहें) standard practice है, mod_rewrite lesson वाले HTTPS-forcing rule के साथ।

Rewrite बनाम Redirect

यह देखना worth है कि किसी rule को actually किसकी ज़रूरत है: एक internal rewrite ([R] flag के बिना) browser के address bar को बिल्कुल भी बदले बिना Apache जो serve करता है उसे बदल देता है -- एक clean-URL front controller के लिए सही। एक redirect ([R=301] या [R=302]) browser को जाने के लिए एक actual नया address भेजता है -- users और search engines दोनों को यह बताने के लिए सही कि कोई URL permanently move हो गया है।

Note: 301 (permanent) browsers और search engines को अपने records update करने और पुराना URL मांगना बंद करने के लिए कहता है; 302 (temporary) नहीं कहता -- genuinely permanent move के लिए 301 इस्तेमाल करें, किसी short-term या reversible चीज़ के लिए 302।

उदाहरण: A framework front-controller rewrite plus canonical www redirect

apacheconf
RewriteEngine On

# Force www.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

# Front controller: send anything that isn't a real file/dir to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
{# Flagged by hand after confirming a runner can't handle this example (a shell command / go.mod file stored as a TopicExample, a language feature the configured runner version doesn't support, or output that blows a runner's sandbox limit) -- see TopicExample.norun. Never render the run button for these, regardless of language, since it would just fail at execute_code (or worse, hang the Judge0 queue on a submission that can never finish cleanly). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. जहाँ असल में एक redirect चाहिए था वहाँ internal rewrite इस्तेमाल करना (या इसका उल्टा), browser का address bar गलत URL दिखाते हुए छोड़ना, या search engines को कभी पता न चलना कि page move हुआ है।
  2. एक permanent move के लिए 302 इस्तेमाल करना, ताकि search engines पुराने URL को index रखें बजाय इसकी ranking नए पर transfer करने के।
  3. एक front-controller rewrite लिखना जो real files (जैसे images या CSS) की requests भी intercept कर ले, जो फिर सीधे serve होने की बजाय application पर route हो जाती हैं।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.