.htaccess फ़ाइल जनरेटर

सामान्य सर्वर कॉन्फ़िगरेशन के लिए Apache .htaccess नियम उत्पन्न करें।

कोई डेटा आपके डिवाइस से नहीं जाता

HTTPS बाध्य करें

WWW / गैर-WWW बाध्य करें

सुरक्षा हेडर

GZIP कंप्रेशन

ब्राउज़र कैशिंग

कस्टम त्रुटि पेज

डायरेक्टरी विकल्प

जनरेट की गई .htaccess फ़ाइल

.htaccess क्या है?

.htaccess फ़ाइल Apache वेब सर्वर के लिए कॉन्फ़िगरेशन फ़ाइल है। यह URL पुनर्निर्देशन, सुरक्षा नियम, पहुँच नियंत्रण और प्रदर्शन अनुकूलन परिभाषित करने देती है।

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

.htaccess फ़ाइल कहाँ रखें?

इसे अपनी साइट की रूट डायरेक्टरी में रखें (आमतौर पर public_html या www)। नियम उस डायरेक्टरी और उप-डायरेक्टरी पर लागू होते हैं।

क्या यह Nginx के साथ काम करेगा?

नहीं। .htaccess Apache-विशिष्ट है। Nginx nginx.conf या उपलब्ध साइट फ़ाइलों में अपने स्वयं के कॉन्फ़िगरेशन सिंटैक्स का उपयोग करता है।

क्या .htaccess मेरी साइट को धीमा कर सकता है?

Apache प्रत्येक अनुरोध पर .htaccess जाँचता है, इसलिए बहुत बड़ी फ़ाइलें ओवरहेड जोड़ सकती हैं। उच्च ट्रैफ़िक साइटों के लिए जाँच न्यूनतम रखें।

What .htaccess actually is

An .htaccess file is Apache's per-directory configuration file. The name is widely understood as a contraction of "hypertext access," reflecting the file's original primary use of restricting access to hypertext documents. Place one in any directory and the directives apply to that directory and every subdirectory beneath it. The file has no name before the dot (it's a Unix dot-file, hidden by default) and it must be named exactly .htaccess (lowercase, leading period) for Apache to find it under the default AccessFileName setting.

There's a real performance cost. Apache's documentation is explicit: "if AllowOverride is set to allow the use of .htaccess files, Apache will look in every directory for .htaccess files. Thus, permitting .htaccess files causes a performance hit, whether or not you actually even use them." The directory tree gets walked on every HTTP request. For a request to /var/www/html/foo/bar/baz.html, Apache stat-checks /.htaccess, then /var/.htaccess, then /var/www/.htaccess, then /var/www/html/.htaccess, and so on, even when none of those files exist. Apache's own recommendation is to use main-config <Directory> blocks where possible (parsed once at startup), and reserve .htaccess for hosting environments where you don't have main-config access.

When you need it (and when you don't)

Apache lists two legitimate use cases for .htaccess: hosting providers giving users limited control without full server-config access, and cases where directives need to differ between subdirectories where the deployer controls only the file system. Outside those, the documentation explicitly recommends moving rules to <Directory> blocks in the main config.

nginx (the second-most-popular web server) does not support .htaccess at all; it deliberately rejects per-directory configuration files for performance reasons, and all configuration lives in nginx.conf and any included site files. If your project is on nginx, this generator's output won't help, you'll need server / location blocks in the nginx config file. Caddy uses its own Caddyfile syntax with automatic HTTPS via Let's Encrypt as the default. LiteSpeed and OpenLiteSpeed support .htaccess for Apache compatibility. IIS uses web.config.

URL rewriting and the canonical HTTPS pattern

Most non-trivial .htaccess work uses mod_rewrite, Apache's rule-based URL rewriting engine built on POSIX extended regular expressions. The basic shape is RewriteRule pattern substitution [flags], optionally preceded by one or more RewriteCond conditions. The standard force-HTTPS block:

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

The [L,R=301] flags mean "this is the last rule in this round" and "respond with HTTP 301 Moved Permanently." A 301 is the right choice for permanent redirects, search engines pass link equity and browsers cache aggressively. Use 302 (Found) only for genuinely temporary redirects, where you don't want the move cached.

Behind a load balancer or reverse proxy that terminates TLS, the origin server sees plain HTTP even when the user's browser is on HTTPS, a naïve RewriteCond %{HTTPS} off rule then redirects forever. The fix is to check the forwarded header instead: RewriteCond %{HTTP:X-Forwarded-Proto} !https. Cloudflare's "Flexible SSL" mode (CDN talks HTTP to origin while user uses HTTPS) is the most common cause of this redirect-loop trap.

Choose between www and non-www canonicalisation with a similar pattern. Pick one canonical hostname for SEO (avoids duplicate-content issues) and for cookie scope (a www subdomain receives different cookies than the apex by default).

Compression and caching

mod_deflate gzip-compresses textual responses, dramatically reducing transfer size on text/HTML/CSS/JS. Wrap the block in <IfModule mod_deflate.c> so it doesn't crash the site on servers where the module isn't loaded. Brotli (mod_brotli) is the modern replacement and uses the same pattern.

mod_expires sets Expires and Cache-Control: max-age headers. The widely-used "access plus 1 year" syntax means the asset can be cached for a year from the moment the user fetched it. Long max-age is correct for hashed asset filenames (app.abc123.js); shorter max-age is correct for HTML and anything mutable. For finer control, Header set Cache-Control "public, max-age=31536000, immutable" on hashed files is the modern best practice.

Security headers worth knowing

Hide what shouldn't be served

Two security-hardening one-liners pay for themselves:

Apache 2.4 replaced the older Order Allow,Deny syntax with the cleaner Require family of directives, Require all granted, Require all denied, Require ip 192.0.2.0/24, Require not ip 198.51.100.5. The old syntax still works for backward compatibility but is documented as deprecated.

Common gotchas

More questions

Where exactly should the file go?

In the directory whose contents you want to control. Most shared-hosting users put it in their site's document root, public_html/, www/, or htdocs/ depending on the host. Apache then walks the directory tree applying the rules to that directory and every subdirectory beneath it.

301 or 302, which redirect should I use?

301 Moved Permanently for permanent moves. Search engines transfer link equity to the new URL; browsers cache the redirect aggressively. Use this for "we moved this page forever" cases. 302 Found for temporary redirects, the browser doesn't cache and search engines don't transfer ranking. Use 302 only when the move really is temporary; defaulting to 302 instead of 301 is one of the most common SEO own-goals.

How do I password-protect a directory?

Create an .htpasswd file with the htpasswd command-line utility (it stores username:bcrypt-hash lines) and store it outside your web root. Then in .htaccess: AuthType Basic, AuthName "Restricted Area", AuthUserFile /full/path/to/.htpasswd, Require valid-user. The browser shows a system Basic-auth prompt to anyone visiting the directory. This is the classic Apache pattern; for serious authentication consider an application-level login system instead.

Will this work on WordPress?

WordPress ships with its own .htaccess block (between # BEGIN WordPress and # END WordPress markers) that handles permalinks. WordPress will overwrite anything inside those markers when you save permalinks settings. Add your own rules outside the WordPress markers (typically above them) so they survive automatic regeneration.

Does anything get sent to a server?

No. The generator is JavaScript that assembles directive blocks based on your checkboxes; the output is built in your browser. Nothing about your domain or chosen options leaves the page; the tool works offline once it's loaded.

संबंधित टूल