Web hosting

htaccess file (.htaccess)

Definition

The .htaccess file is a configuration file read by the Apache web server, and by LiteSpeed, in the directory where it sits and in all its subdirectories. It lets you change settings without access to the main configuration: URL rewriting for WordPress permalinks, redirects, access restrictions, headers or caching.

htaccess file (.htaccess), Web hosting

The name starts with a dot, which hides it by default in many FTP clients and file managers. The .htaccess file belongs to the Apache world: LiteSpeed reads it too, but Nginx, Caddy and IIS ignore it. It's the basic tool of shared hosting, where you have no access to the server configuration.

How Apache reads .htaccess

The Apache documentation presents it as a way to change the configuration on a per-directory basis for users who don't have root access. Everything depends on the AllowOverride directive in the main configuration: its default value is None, in which case .htaccess files are completely ignored. Hosts enable it to allow permalinks, redirects and access restrictions.

Diagram of how Apache reads .htaccess files for a request to /wp-content/uploads/photo.jpg. Apache first applies the main configuration, where AllowOverride allows .htaccess files or not. It then reads the .htaccess at the site root, with the WordPress rules, then the one in wp-content, then the one in uploads, for example to block PHP execution. The rules add up, those of the deepest directory winning in case of conflict, and these files are read again on every request.

When they're allowed, Apache looks for an .htaccess in every directory along the requested path and rereads these files on every request. A change therefore takes effect immediately, without a restart, but at the cost of extra file reads. Hence the documentation's advice: if you have access to the main configuration, put your rules there rather than in .htaccess files.

The WordPress block

WordPress writes the rewrite rules that make permalinks work into the root .htaccess:

# BEGIN WordPress
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

Anything between the # BEGIN WordPress and # END WordPress markers can be rewritten by WordPress, for example when you save the permalink settings. Put your own rules outside these markers, and the same goes for plugins, which add their own blocks.

Useful protections

  • Block direct access to wp-config.php and to forgotten sensitive files, such as .sql backups, the .git folder or debug.log, which we cover in our article on exposed sensitive files.
  • Prevent PHP execution in wp-content/uploads with an .htaccess dedicated to that folder.
  • Restrict wp-admin by IP address or with an HTTP password, as in our guide to protecting wp-admin at the server level.
  • Limit hotlinking of your images based on the Referer header.

A syntax error immediately triggers a 500 error for the whole directory: keep a copy of the file before every change.

For WordPress

On an Nginx server, an .htaccess file has no effect at all: its rules have to be translated into the nginx.conf file, which often requires help from the host. Yet many cache and security plugins assume .htaccess is there. WP Feint relies on it only for the file aliases of the Pro version, for which it generates rules for each server, and to hide its own folder: the compatible servers and caches page explains where to put these rules, including on o2switch, where they must come before the LiteSpeed Cache block.

Further reading

  • Article Exposed sensitive files: .env, .git, .sql backups and debug.log
  • Page Host and cache compatibility

Sources

  1. Apache HTTP Server: .htaccess files (external link, opens in a new tab)
  2. WordPress Advanced Administration: Apache HTTPD / .htaccess (external link, opens in a new tab)
  3. Apache HTTP Server: When not to use mod_rewrite (external link, opens in a new tab)

All definitions

Start free, go Pro when you’re ready

The free version already hides WordPress and moves the login page. Pro adds aliases, two-factor authentication and monitoring.