← Back to Apache Course | Chapter 3: Modules | Lesson 1 of 5

mod_rewrite का उपयोग

mod_rewrite Apache का URL-rewriting engine है -- यह आपको pattern-matching rules के आधार पर एक आने वाली request को एक अलग internal URL पर rewrite करने देता है, जिससे sites /product.php?id=42 को address bar में एक clean /products/42 में बदल पाती हैं।

Module Enable करना

mod_rewrite हमेशा default से on नहीं होता। Ubuntu/Debian पर, इसे sudo a2enmod rewrite से enable करें और Apache reload करें; RHEL-family systems पर यह आमतौर पर compiled in होता है और बस config में LoadModule rewrite_module modules/mod_rewrite.so uncomment करने की ज़रूरत होती है।

RewriteEngine और RewriteRule

RewriteEngine On उस scope (VirtualHost, Directory, या .htaccess) के लिए rewriting on कर देता है। हर RewriteRule pattern substitution [flags] फिर requested path को एक regular expression से match करता है और, अगर match हो, इसे substitution से replace करता है। उदाहरण के लिए, RewriteRule ^products/([0-9]+)/?$ product.php?id=$1 [L] /products/42 को internally product.php?id=42 पर भेजता है।

RewriteCond: Conditionally Rewrite करना

RewriteCond नीचे वाले RewriteRule के apply होने से ठीक पहले एक condition add करता है जो true होनी चाहिए -- request की scheme, host, या कोई file पहले से exist करती है या नहीं जैसी चीज़ें check करते हुए। एक बहुत common pattern HTTPS force करता है: RewriteCond %{HTTPS} off जिसके बाद एक rule जो same URL के https:// version पर redirect करता है।

आम Flags

[brackets] में flags यह बदलते हैं कि एक rule कैसे behave करता है। [L] (last) एक बार यह match हो जाए तो आगे के rules process करना बंद कर देता है। [R] या [R=301] internal rewrite की बजाय एक actual browser redirect issue करता है, और number HTTP status code set करता है। [NC] pattern को case-insensitive बनाता है। [QSA] original query string को discard करने की बजाय rewritten URL में append कर देता है।

Warning: [L] flag छोड़ देना Apache को match होने के बाद भी rules evaluate करते रहने देता है, जो URL को दूसरी बार ऐसे rewrite कर सकता है जो आपका इरादा नहीं था -- लगभग हर real-world rule set को इसकी ज़रूरत होती है।

उदाहरण: Clean URLs and forcing HTTPS with mod_rewrite

apacheconf
RewriteEngine On

# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# /products/42 -> product.php?id=42
RewriteRule ^products/([0-9]+)/?$ product.php?id=$1 [L,QSA]
{# 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. scope के top पर RewriteEngine On भूल जाना -- इसके बिना नीचे का हर RewriteRule चुपचाप ignore हो जाता है।
  2. [L] flag छोड़ देना, जिससे बाद का कोई rule पहले से match हुए URL को और rewrite कर देता है, confusing double-rewritten results देते हुए।
  3. .htaccess में सीधे rules लिखना जब उस directory के लिए AllowOverride FileInfo permit नहीं करता, ताकि rules parse तो हों लेकिन असल में कभी effect में न आएं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 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.