Key facts
- Plain text containing Apache directives such as RewriteRule, Redirect, Header and AuthType; it applies to its directory and all subdirectories.
- Read on every request, so changes take effect immediately without a restart, but this costs some performance.
- Only works when the main config sets AllowOverride for that directory; Nginx ignores .htaccess files completely.
- Rewrite rules need mod_rewrite enabled and RewriteEngine On; header rules need mod_headers.
- Created by developers, hosting control panels and CMSs such as WordPress, which writes its permalink block between # BEGIN WordPress and # END WordPress.
How to open a .htaccess file
Download it via FTP/SFTP or your host's file manager and edit it in Notepad++ or VS Code. Enable 'show hidden files' in FileZilla (Server > Force showing hidden files) if you cannot see it.
Press Cmd+Shift+. in Finder to show hidden files, then edit it in a code editor or with nano .htaccess in Terminal.
Edit it on the server with nano /var/www/html/.htaccess (adjust the path) and watch /var/log/apache2/error.log for errors after saving.
Common problems and fixes
- 500 Internal Server Error right after editing
- A typo or an unsupported directive breaks the whole directory. Restore the previous version, then check the Apache error log, which names the bad line (for example 'Invalid command').
- Redirect loop / ERR_TOO_MANY_REDIRECTS
- An HTTP-to-HTTPS or www rule keeps matching its own target, often behind a proxy or CDN that terminates SSL. Add a condition such as RewriteCond %{HTTPS} off, or check X-Forwarded-Proto when behind a proxy.
- Rules are ignored completely
- AllowOverride is None, mod_rewrite is not enabled, or the server is actually Nginx. Ask your host or enable AllowOverride All and a2enmod rewrite on your own server.
- WordPress permalinks return 404
- The WordPress rewrite block is missing or the file is not writable. Re-save Settings > Permalinks or restore the standard # BEGIN WordPress block.
Often converted to or from: Nginx config, Apache VirtualHost config, web.config (IIS)