All posts

Blog

.htaccess Guide for Beginners: What It Is and How to Use It Safely

Illustrated cover for the Developers guide ".htaccess Guide for Beginners: What It Is and How to Use It Safely"

There's a small, hidden file in many website folders that has surprising power. With a few lines of text it can force HTTPS, redirect old pages, block troublesome visitors, password-protect a folder or speed up your site. Get one character wrong, though, and it can take your whole site offline with a "500 Internal Server Error."

That file is .htaccess. This guide explains what it is, how to edit it without breaking anything, and the practical rules most website owners actually need.

What is .htaccess?

.htaccess (short for "hypertext access") is a configuration file used by the Apache web server and by servers compatible with it, such as LiteSpeed. It lets you change how the server behaves for a specific folder and everything inside it, without needing access to the main server configuration.

Key points:

  • The name starts with a dot, which makes it a hidden file on Linux servers.
  • It applies to the folder it's in and all subfolders.
  • A .htaccess file in a subfolder can add to or override rules from a parent folder.
  • Changes usually take effect immediately, with no server restart needed.

Nginx doesn't use .htaccess. If your site runs on Nginx alone, similar rules go in the server configuration instead.

Where to find it

On most shared hosting accounts, your main site's .htaccess file lives in public_html.

Because it's hidden, you may need to reveal it:

  • cPanel File Manager: click Settings and enable Show Hidden Files (dotfiles).
  • DirectAdmin File Manager: hidden files are usually visible by default.
  • FTP programs: look for an option to show hidden files.

If there's no .htaccess file, you can create one. It's a plain text file named exactly .htaccess, with no extension.

Before you edit: safety first

  1. Make a backup copy. Download the file or duplicate it as .htaccess.bak before changing anything.
  2. Make one change at a time. Then test your site in a private browser window.
  3. Use a plain text editor. Word processors can add hidden formatting that breaks the file.
  4. Keep the WordPress block intact. WordPress manages the section between # BEGIN WordPress and # END WordPress. Put your own rules above it.

If your site shows a 500 error after a change, restore the backup right away. Our guide to fixing WordPress 404 errors and a broken .htaccess file covers recovery in more detail.

How the syntax works

.htaccess files are made up of directives, one per line. Lines that begin with # are comments and are ignored.

# This is a comment
Options -Indexes

Many rules use mod_rewrite, Apache's URL rewriting module. Rewrite rules usually follow this pattern:

RewriteEngine On
RewriteCond <condition>
RewriteRule <pattern> <target> [flags]
  • RewriteEngine On turns rewriting on.
  • RewriteCond sets a condition that must be true.
  • RewriteRule matches the requested path and decides where it goes.
  • Flags like [L,R=301] mean "last rule" and "redirect permanently."

Useful rules for beginners

Force HTTPS

Send all visitors to the secure version of your site:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

Make sure your SSL certificate works first. See what SSL and HTTPS are. If your site sits behind a CDN or proxy, this rule can bounce visitors back and forth endlessly and cause an ERR_TOO_MANY_REDIRECTS error. In that case, use the CDN's HTTPS setting instead.

Redirect www to non-www (or the reverse)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]

Pick one version of your domain and stick with it.

Redirect a single page

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

For more on when and how to redirect, see 301 redirects and SEO.

Turn off directory listing

If a folder has no index file, some servers show a list of every file inside it. Turn that off:

Options -Indexes

Set a custom error page

ErrorDocument 404 /404.html

Block an IP address

On servers running Apache 2.4 or later:

<RequireAll>
  Require all granted
  Require not ip 203.0.113.45
</RequireAll>

Replace the example address with the one you want to block. Blocking by IP is a blunt tool, since many visitors share addresses, so use it sparingly.

Protect a sensitive file

For example, to block direct access to WordPress's wp-config.php:

<Files wp-config.php>
  Require all denied
</Files>

Password-protect a folder

You can do this by hand with AuthType directives and a .htpasswd file, but it's easier to use your control panel. In cPanel, look for Directory Privacy. DirectAdmin has a Password Protected Directories tool. Both write the .htaccess rules for you.

Enable browser caching

Tell browsers to keep static files longer so repeat visits load faster:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
</IfModule>

Wrapping rules in <IfModule> means they're skipped if the module isn't available, instead of causing an error. Many caching plugins add rules like these automatically, so check before adding your own to avoid duplicates. See types of website caching explained for the bigger picture.

Settings you might not control

Some hosts let you change PHP settings like memory_limit or upload_max_filesize in .htaccess with php_value lines. On many modern setups, though, those lines cause a 500 error because PHP runs differently. Use your control panel's PHP settings tool instead, which is safer.

Common mistakes

  • Typos and missing characters. One stray character can break the whole file.
  • Conflicting redirects. Two rules sending traffic back and forth create a redirect loop.
  • Editing inside the WordPress block. WordPress may overwrite your changes.
  • Copying rules without understanding them. Add only what you need, and comment each rule so you remember why it's there.
  • Too many files. Scattered .htaccess files in many subfolders make problems hard to track down.

How to test changes

  • Load your homepage and a few inner pages in a private window.
  • Test old URLs to confirm redirects go to the right place in one hop.
  • Check your control panel's error log if something fails. It often names the exact line causing the problem.

Wrapping up

.htaccess is a small file with a lot of influence over how your website behaves. Back it up before every edit, change one thing at a time, keep your own rules above the WordPress block and test after each change. Used carefully, it's one of the most useful tools you have for redirects, security and speed.

Related reading: How to Fix ERR_TOO_MANY_REDIRECTS (Redirect Loop) on Your Website.