What is the .htaccess file?

.htaccess is a configuration file for the Apache web server, and for compatible servers such as LiteSpeed, that lives inside a site's folders and applies rules to that directory and everything below it. It is read on every request, so changes take effect at once, with no server restart.

For SEO it is where redirects, pretty URLs, HTTPS enforcement and response headers are often defined. One typing error in it can take a whole site offline.

What WordPress puts in it

On Apache, WordPress writes this block to the .htaccess file in the site root when you save a permalink structure:

apache
# BEGIN WordPress
<IfModule mod_rewrite.c>
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]
</IfModule>
# END WordPress

It says: if the requested path is not a real file or folder, hand the request to index.php. That one rule is what makes /blog/my-post/ work. WordPress regenerates everything between the two marker comments, so custom rules go above or below the block, never inside it.

If posts return 404 while the homepage loads, this block is missing or the file is not writable. Re-saving Settings → Permalinks rewrites it.

SEO jobs it commonly does

Force HTTPS and a single hostname:

apache
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]

Redirect one moved page:

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

Keep PDFs out of the index with an X-Robots-Tag:

apache
<FilesMatch "\.pdf$">
  Header set X-Robots-Tag "noindex"
</FilesMatch>

It can also set cache lifetimes and compression for static files, both of which affect page speed.

Limits and risks

  • Apache only. Nginx does not read .htaccess at all. The same rules go in the server configuration, which your host manages. If your site runs on Nginx, a guide telling you to paste lines into .htaccess does not apply.
  • A syntax error means a 500 error on every page. Keep a copy of the working file before editing and know how to restore it over SFTP.
  • It costs a little on every request, because the server looks for and parses the file each time. Hosts with full control put rules in the main configuration instead.
  • Rule order matters. Redirects placed after the WordPress block may never run, because the request has already been handed to index.php.
  • Hundreds of redirect lines become unmanageable. Past a certain size, a redirect list is easier to maintain in a tool that records what each rule is for.

Do you need to edit it?

Often not. Redirects created in a plugin are served by WordPress and work on any server; Hydrogen SEO's redirection manager handles 301 and 302 rules that way. Server-level rules are still the right place for site-wide HTTPS and hostname redirects, since they run before PHP starts, and for headers on files WordPress does not serve itself. Our redirect rule generator writes Apache and Nginx versions of a redirect list and checks it for chains.

Common questions

Where is the .htaccess file in WordPress?

In the site's root folder, next to wp-config.php. File names starting with a dot are hidden by default, so enable "show hidden files" in your FTP client or file manager.

Does Nginx use .htaccess?

No. Nginx ignores the file. Equivalent rules are written in the Nginx server configuration, usually by the host.

Can editing .htaccess break my site?

Yes. An invalid line causes a 500 Internal Server Error across the site. Restoring the previous version of the file fixes it immediately.