← Back to Apache Course | Chapter 2: Configuration Basics | Lesson 4 of 4

.htaccess फ़ाइल

एक .htaccess file आपको किसी folder में एक plain text file रखकर उस folder पर Apache configuration apply करने देती है -- main server config तक access की ज़रूरत नहीं, यही कारण है shared hosting इस पर इतना heavily depend करती है।

.htaccess किस लिए है

shared hosting पर, customers आमतौर पर httpd.conf edit नहीं कर सकते -- उनके पास server access बिल्कुल नहीं होता, बस अपनी files से एक FTP/SFTP connection होता है। किसी भी directory में डाली गई .htaccess file उन्हें उस directory (और इसकी subdirectories) के लिए redirects, custom error pages, access rules, और rewrite rules set करने देती है बिना इससे ज़्यादा कुछ चाहिए।

यह सिर्फ तब काम करता है जब AllowOverride कहे

एक .htaccess file अपने आप कुछ नहीं करती -- Apache इसे तभी पढ़ता है जब main config में enclosing <Directory> का AllowOverride उस context के लिए None के अलावा कुछ और set हो। AllowOverride All इसे कुछ भी override करने देता है; AllowOverride FileInfo AuthConfig जैसे ज़्यादा specific values यह restrict करते हैं कि यह directives की किन categories को छू सकता है।

Warning: सबसे common .htaccess support question है "मेरा .htaccess कुछ क्यों नहीं कर रहा" -- और जवाब लगभग हमेशा उस directory पर AllowOverride None होता है।

आम इस्तेमाल

classic इस्तेमाल clean URLs के लिए URL rewriting है (mod_rewrite और URL Rewriting lessons में पूरी तरह covered), plus custom error pages (ErrorDocument 404 /404.html), AuthType Basic से किसी directory को password-protect करना, पुराने URLs को नए पर redirect करना, और static assets के लिए caching headers set करना।

Performance की Cost

क्योंकि .htaccess files किसी भी directory और इसकी subdirectories में रह सकती हैं, Apache को लगभग हर single request पर एक check करना पड़ता है, हर बार directory tree ऊपर चलते हुए। अगर आपके पास server सीधे control है, तो वही rules main config में सीधे किसी <Directory> block (या VirtualHost) में डालना तेज़ है, क्योंकि Apache को इसे सिर्फ startup पर एक बार parse करना है, हर request पर नहीं।

Note: अगर आपके पास पूरा server access है, तो .htaccess की जगह VirtualHost/Directory config में सीधे rules डालना prefer करें -- .htaccess को उन cases के लिए रखें जहाँ आपके पास genuinely main config तक access नहीं है।

Inheritance और Precedence

.htaccess rules उस directory और इसके नीचे की हर चीज़ पर apply होते हैं, और किसी subdirectory में एक .htaccess file tree में ऊपर वाली एक के ऊपर layer होती है (और उसे override कर सकती है) -- main config में <Directory> blocks जैसा ही same nesting behavior, बस एक file की जगह filesystem में distributed।

उदाहरण: A typical .htaccess file

apacheconf
# .htaccess in the site's document root

# Custom error page
ErrorDocument 404 /404.html

# Redirect an old URL to a new one
Redirect 301 /old-page.html /new-page.html

# Prevent this folder's contents from being listed
Options -Indexes
{# 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. यह check किए बिना कि AllowOverride उस directory के लिए actually इसकी permission देता है, .htaccess में rules add करना और कुछ न होने पर confuse होना।
  2. performance-sensitive, server-wide rules के लिए .htaccess इस्तेमाल करना जब आपके पास httpd.conf तक पूरा access है -- हर request Apache के directory tree ऊपर .htaccess files के लिए दोबारा check करने की cost pay करता है।
  3. .htaccess में एक syntax error (main config में syntax error के उलट) उस directory की हर request पर तुरंत एक 500 Internal Server Error produce कर सकती है, बिना पहले इसे पकड़ने के लिए कोई configtest step के।
चैप्टर सारांश
  • httpd.conf (और इसके Include किए गए files) Apache का central configuration है, जो Listen करने वाला port और DocumentRoot जैसी चीज़ें set करता है, और reload करने से पहले हमेशा configtest से check किया जाना चाहिए।
  • Virtual Hosts एक Apache server को कई independent sites host करने देते हैं, जिन्हें browser द्वारा भेजे Host header से match किया जाता है, जबकि Directory blocks और .htaccess specific folders पर rules -- access control, Options, rewrite rules -- apply करते हैं, और .htaccess उन cases के लिए option है जब आप main config सीधे edit नहीं कर सकते।
🔒

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.