Apache के लिए .htaccess जेनरेटर

Apache .htaccess नियम बनाएं: HTTPS ज़बरदस्ती, www जोड़ना या हटाना, 301 रीडायरेक्ट, एरर पेज, gzip, ब्राउज़र कैशिंग, सिक्योरिटी हेडर और dotfile ब्लॉक।

स्टेटस 301, 302, 303, 307, 308 या 410 (हटाया गया) हो सकता है। नया पाथ पूरा URL भी हो सकता है।

.htaccess
<IfModule mod_rewrite.c>
  RewriteEngine On
  # Redirect HTTP to HTTPS
  RewriteCond %{HTTPS} off
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# Redirects (mod_alias)
Redirect 301 /old-page /new-page
Redirect 301 /blog/2019 /blog

# Custom error pages
ErrorDocument 404 /404.html

# Disable directory listings
Options -Indexes

# Block access to hidden files such as .env and .git
<FilesMatch "^\.">
  Require all denied
</FilesMatch>
RedirectMatch 404 /\.git

# Security headers
<IfModule mod_headers.c>
  Header always set X-Content-Type-Options "nosniff"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# Compress text responses
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
  AddOutputFilterByType DEFLATE application/javascript application/json application/xml application/rss+xml
  AddOutputFilterByType DEFLATE image/svg+xml font/ttf font/otf
</IfModule>

# Browser caching
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 month"
  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/javascript "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/avif "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/x-icon "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
</IfModule>
mod_rewrite, mod_alias, mod_headers, mod_deflate और mod_expires वाले Apache 2.4 के लिए (जहाँ संभव हो ब्लॉक IfModule में रखे गए हैं)। पहले स्टेजिंग सर्वर पर जाँचें – .htaccess में एक सिंटैक्स त्रुटि पूरे फ़ोल्डर पर HTTP 500 देती है। Nginx .htaccess नहीं पढ़ता।

.htaccess जेनरेटर क्या है?

यह टूल Apache 2.4 के लिए इस्तेमाल को तैयार .htaccess फ़ाइल बनाता है। जो फ़ीचर चाहिए उन्हें टिक करें – HTTPS रीडायरेक्ट, www या non-www canonical होस्ट, HSTS, डायरेक्टरी लिस्टिंग बंद, छिपी फ़ाइलों की सुरक्षा, gzip कम्प्रेशन, ब्राउज़र कैशिंग, बुनियादी सिक्योरिटी हेडर – और अपने 301/302/410 रीडायरेक्ट व कस्टम एरर पेज जोड़ें।

.htaccess जेनरेटर का उपयोग कैसे करें

  1. जो विकल्प चाहिए उन्हें टिक करें और चुनें कि www को कैसे संभालना है।
  2. रीडायरेक्ट /old-path /new-path के रूप में डालें, चाहें तो उसके बाद स्टेटस (301, 302, 303, 307, 308 या 410) लिखें।
  3. कस्टम 404 और 500 पेज के पाथ डालें।
  4. नतीजा .htaccess के रूप में कॉपी या डाउनलोड करें और पहले स्टेजिंग सर्वर पर टेस्ट करें।

उदाहरण

इनपुट
Force HTTPS, remove www, redirect /old-page → /new-page
आउटपुट
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
Redirect 301 /old-page /new-page

.htaccess जेनरेटर का उपयोग कब करें

इसे शेयर्ड होस्टिंग या ऐसे Apache सर्वर पर इस्तेमाल करें जहाँ आप मुख्य सर्वर कॉन्फ़िग एडिट नहीं कर सकते। Nginx, LiteSpeed की अपनी सेटिंग, Cloudflare या CDN पर उनके अपने कॉन्फ़िगरेशन इस्तेमाल करें – Nginx .htaccess को पूरी तरह अनदेखा करता है। सिंटैक्स एरर पूरी डायरेक्टरी के लिए HTTP 500 देता है, इसलिए हमेशा टेस्ट करें।

.htaccess जेनरेटर कैसे काम करता है

HTTPS और www नियम <IfModule mod_rewrite.c> के अंदर mod_rewrite (RewriteCond %{HTTPS} off और होस्ट कंडीशन) इस्तेमाल करते हैं; HTTPS ज़बरदस्ती लागू हो तो www रीडायरेक्ट सीधे https इस्तेमाल करता है, जिससे दो-हॉप चेन नहीं बनती। पाथ रीडायरेक्ट mod_alias की Redirect लाइनें इस्तेमाल करते हैं (410 के लिए Redirect gone)। कम्प्रेशन mod_deflate, कैशिंग mod_expires (स्टैटिक एसेट के लिए लंबी अवधि और HTML के लिए नहीं), और हेडर Header always set के साथ mod_headers इस्तेमाल करते हैं। Dotfile <FilesMatch "^\."> और RedirectMatch 404 /\.git से रोकी जाती हैं।

सामान्य उपयोग मामले

  • सर्टिफ़िकेट इंस्टॉल करने के बाद HTTPS और एकल non-www होस्ट ज़बरदस्ती लागू करना।
  • साइट रीडिज़ाइन के बाद पुराने URL रीडायरेक्ट करना।
  • शेयर्ड होस्टिंग पर WordPress साइट तेज़ करने के लिए कम्प्रेशन और कैशिंग चालू करना।
  • .env और .git फ़ाइलों तक पहुँच ब्लॉक करना।

अक्सर पूछे जाने वाले प्रश्न

.htaccess अपलोड करने के बाद 500 एरर क्यों आता है?

आमतौर पर कोई ज़रूरी मॉड्यूल सक्षम नहीं है या कोई डायरेक्टिव AllowOverride से अनुमत नहीं है। Apache एरर लॉग देखें।

क्या मुझे HSTS चालू करना चाहिए?

तभी जब पूरी साइट और सभी सबडोमेन HTTPS पर चलते हों – ब्राउज़र max-age की अवधि तक सादे HTTP को अस्वीकार करेंगे।

क्या .htaccess Nginx पर काम करता है?

नहीं। Nginx .htaccess नहीं पढ़ता; नियमों को server block में बदलना होता है।

और श्रेणियाँ देखें