Skip to content
Log In Sign Up info@muvcloud.com

What .htaccess Does

.htaccess is a plain text file that Apache looks for in the folders of a website. The directives in it apply to that folder and everything below it, as if they were written in a Directory block of the main configuration. Applications such as WordPress ship one because many of their users cannot edit the server configuration.

Nginx has no equivalent. It reads its configuration only at start and reload and ignores .htaccess files, so rules written for them must be translated into the server block. See Apache vs Nginx.

Enable It with AllowOverride

With AllowOverride None, Apache does not read .htaccess at all. That is not an error: the file is silently ignored. It is the default for /var/www/ on Debian and Ubuntu and for /var/www/html on AlmaLinux and Rocky Linux. To enable it for one site, set it in that site's virtual host:

<Directory /var/www/example.com/public_html>
    AllowOverride All
    Require all granted
</Directory>
sudo apachectl configtest
sudo systemctl reload apache2    # httpd on AlmaLinux and Rocky Linux

AllowOverride All permits everything. To permit only some groups, list them: FileInfo covers rewrites, redirects, headers, and error pages; AuthConfig covers password protection and Require; Options covers the Options directive.

Common Uses

Redirect a Page

Redirect 301 /old-page https://example.com/new-page

Send All Traffic to HTTPS

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

This needs mod_rewrite; see how to enable mod_rewrite in Apache.

Allow Only One Address

Require ip 203.0.113.10

Block One File

<Files "config.php">
    Require all denied
</Files>

Add a Header

Header set X-Content-Type-Options "nosniff"

Header needs mod_headers: run sudo a2enmod headers on Debian and Ubuntu; AlmaLinux and Rocky Linux load it by default.

Custom Error Page and No Folder Listings

ErrorDocument 404 /404.html
Options -Indexes

The Performance Cost

When overrides are allowed, Apache has to look for a .htaccess file in every folder on the path to the requested file, on every request, and read each one it finds. A request for /blog/2026/post.php means a lookup in the document root, in blog, and in 2026, even if none of them has the file. On a busy site, these lookups add work to each request. The main configuration, by contrast, is read once, at start or reload.

Better: Put the Rules in the Virtual Host

On a server you control, move the rules into a Directory block of the virtual host and set AllowOverride None. They work the same way there, configtest checks them before they go live, and they cost nothing per request. Keep .htaccess for applications that write it themselves, such as WordPress permalinks, and for folders managed by people who have no access to the server configuration.

When .htaccess Causes a 500 Error

One wrong line returns 500 Internal Server Error for the whole folder. Follow the error log while you load the page:

sudo tail -f /var/log/apache2/error.log    # Debian and Ubuntu
sudo tail -f /var/log/httpd/error_log      # AlmaLinux and Rocky Linux

The log line names the .htaccess file. Two causes cover most cases:

  • Invalid command: the module behind the directive is not loaded, for example RewriteEngine without mod_rewrite, or Header without mod_headers. Enable the module, test, and restart Apache, as a2enmod asks.
  • Not allowed here: AllowOverride does not permit that group of directives. Widen AllowOverride for the folder, or move the rule into the virtual host.

Other causes are covered in 500 Internal Server Error.

Permissions and Visibility

Apache must be able to read the file; mode 644 is right. The default configuration on both families refuses to serve files whose names start with .ht, so visitors cannot download your rules.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations