.htaccess Generator
Generate Apache .htaccess rules for redirects, rewrites, security headers, caching, compression, and more.
What is .htaccess?
.htaccess is a configuration file used by Apache web servers to control directory-level settings like redirects, URL rewriting, security, and caching without modifying the main server config.
Where should I place .htaccess?
Place the .htaccess file in the root directory of your website or in any subdirectory where you want the rules to apply. Rules affect the directory and all subdirectories.
Does .htaccess work with Nginx?
No, .htaccess is specific to Apache. Nginx uses a different configuration syntax. If you use Nginx, you need to convert .htaccess rules to Nginx configuration format.
Can .htaccess rules break my site?
Yes, incorrect .htaccess rules can cause 500 Internal Server Error or broken redirects. Always test rules and keep a backup of your working .htaccess file.
What is the difference between 301 and 302 redirects?
A 301 redirect is permanent and tells search engines to transfer ranking signals to the new URL. A 302 redirect is temporary and doesn't pass ranking signals. Use 301 for permanent moves and 302 for temporary changes.
How do I force HTTPS using .htaccess?
Use RewriteEngine On with RewriteCond %{HTTPS} off and a RewriteRule to redirect all HTTP traffic to HTTPS. This ensures secure connections for all visitors.
What is mod_rewrite?
mod_rewrite is an Apache module that provides powerful URL manipulation capabilities. It allows you to rewrite URLs, create redirects, and control access based on various conditions like hostname, request method, or HTTP headers.
How do I password protect a directory?
Use AuthType Basic, AuthUserFile, and Require valid-user directives in .htaccess. You'll also need to create a .htpasswd file with encrypted passwords using the htpasswd command.
How do I block specific IP addresses?
Use the Require directive with IP addresses: Require not ip 192.168.1.1 blocks a single IP. You can also block IP ranges or entire subnets.
How do I enable browser caching?
Use mod_expires with ExpiresByType directives to set cache expiration times for different file types. This improves site performance by reducing server load.
How do I enable GZIP compression?
Use mod_deflate with AddOutputFilterByType DEFLATE to compress text-based files like HTML, CSS, and JavaScript before sending them to browsers.
What are security headers?
Security headers like X-Frame-Options, X-Content-Type-Options, and Content-Security-Policy protect your site from clickjacking, MIME sniffing, and XSS attacks.
How do I prevent hotlinking?
Use mod_rewrite to check the HTTP_REFERER and block requests from external sites. This prevents other websites from using your images and bandwidth.
How do I create custom error pages?
Use the ErrorDocument directive followed by the error code and path to your custom page, like ErrorDocument 404 /404.html for custom 404 pages.