Skip to content
Atlas Hoster
Guides

The .htaccess File: What It Does and How to Edit It Without Breaking Your Site

By The Atlas Hoster team6 min read

You want to force HTTPS, change your site's home page or block an overly persistent bot, and every answer mentions a file with an odd name: .htaccess. This small text file gives the web server instructions for one folder of your site. Used well, it is very handy. Written badly, a single line is enough to take the site down.

Here is what it is, where to find it, the most common uses with ready-to-copy examples, and the precautions to take before you touch it.

What is .htaccess?

The name comes from "hypertext access". It is a configuration file read by the Apache web server. Apache's documentation describes it as a "distributed configuration file": placed in a folder, it applies its instructions to that folder and all its subfolders (Apache documentation).

Its point: changing how the server behaves without access to its main configuration. That is exactly the situation of shared web hosting, where several sites live on the same server. Apache itself lists hosting run through a control panel such as cPanel among the cases where .htaccess is the right tool.

Our cPanel web hosting runs on Apache, so your site's .htaccess is read on every visit.

Where to find it and how to edit it

The main file sits at the root of your site, in the public_html folder. Because its name starts with a dot, it is hidden: many tools do not show it by default.

In cPanel, open File Manager (in the Files section), click Settings at the top right and tick the option that shows hidden files (dotfiles). The file then appears in public_html; select it and click Edit. Our help article Publish your site with cPanel's File Manager introduces the tool. You can also use an FTP client set to show hidden files.

If it does not exist, you can create it: a plain text file named exactly .htaccess, with no extension.

Before any change, make a copy of the file (for example .htaccess.bak), or download it to your computer. That is your way back.

Redirect the whole site to HTTPS

This is the most requested use. Once your SSL certificate is in place, these three lines send every http:// visit to the same address over https://, with a permanent (301) redirect:

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

Put them at the top of the file. Two notes:

  • recent versions of cPanel also offer a per-domain Force HTTPS Redirect option in the Domains screen, which saves you from writing the rule by hand (cPanel documentation on redirects);
  • if your site sits behind a proxy or CDN that reaches your server over plain HTTP, this rule can loop. In that case, set up the redirect at the proxy.

Why it matters for trust and search rankings: see our article Switching to HTTPS.

Redirect a page or a domain

For an old page that has moved, one line is enough:

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

The simplest option is often cPanel's Redirects tool, which writes the rule into .htaccess for you. It is described step by step in the help article Redirect a page or a domain to another address. cPanel's documentation notes that the tool adds its rules at the bottom of the file, and that some applications such as WordPress may then ignore them: if a redirect does not work on a WordPress site, that is the first thing to check.

Change the home page

By default, the server shows index.html or index.php when someone opens your domain. To pick another file, use the DirectoryIndex directive (Apache documentation, mod_dir):

DirectoryIndex home.html index.php

The server tries the files in order: home.html if it exists, otherwise index.php. If all you want is to replace the home page, renaming your file to index.html is still the simplest solution (Publish your website).

Block an IP address or a bot

To deny access to an address or a range of addresses, the Apache 2.4 syntax is (Apache documentation, access control):

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

The addresses above are examples reserved for documentation: replace them with the ones you want to block. cPanel's IP Blocker does the same without code: it splits ranges into subnets and adds them to .htaccess itself (cPanel documentation). See Block an IP address or a domain.

Never block your own address, and be wary of large ranges: one address is often shared by many people.

Password-protect a folder

A private folder (a staging site, documents for a client) can ask for a username and password. It relies on two files: .htaccess, which requires a login, and a .htpasswd file, which holds the encrypted passwords.

cPanel's Directory Privacy tool creates both for you and stores the password file outside the public folder (cPanel documentation, cPanel support). The steps are in the help article Password-protect a folder on your site. Use this protection together with HTTPS, otherwise the password travels in clear text.

Show a custom error page

Instead of the server's bare 404 page, you can show a page in your own style, with a link back to the home page:

ErrorDocument 404 /error-404.html

Create the error-404.html file at the root of the site first.

Precautions to take

One syntax error breaks the whole folder. A badly written or disallowed directive causes a server error (often "500 Internal Server Error") on the folder and its subfolders. Apache's documentation then advises reading the error log, which names the directive at fault. The quickest fix: put back the copy you made, then try again one change at a time.

Leave the blocks written by your software alone. WordPress manages its own rules between the # BEGIN WordPress and # END WordPress lines, and may rewrite that section (WordPress documentation). cPanel adds a block marked cPanel-generated handler, do not edit, which sets your site's PHP version (cPanel support). Write your rules outside these blocks.

Test in a private window. Browsers remember permanent redirects: an old rule can seem to still apply when it no longer does.

One folder, one file. A .htaccess placed in a subfolder adds to the one at the root. If a rule surprises you, check that another file further down the tree is not overriding it.

If your site shows an error after a change and you cannot find the cause, our article Site Down: What to Do During an Incident helps you keep a cool head, and a backup of your site before big changes is still the best insurance.

At Atlas Hoster

Our web hosting plans run cPanel and Apache: you manage your .htaccess yourself, through File Manager or FTP, and cPanel's tools (Redirects, IP Blocker, Directory Privacy) write the common rules for you. For your site's security beyond .htaccess, see also our Security page and the article Securing a WordPress Site.

Frequently asked questions

I cannot see the .htaccess file. Is that normal?

Yes. Its name starts with a dot, so it is hidden. In cPanel's File Manager, open Settings and show hidden files. If it still is not there, you can create it.

My site shows a 500 error after I edited .htaccess. What should I do?

Put back the copy you made before the change, or remove the lines you last added: if the error came from them, the site displays again. Then redo the change line by line to find the one causing the problem.

Do I need a .htaccess file in every folder?

No. The file in public_html applies to the whole site. A .htaccess in a subfolder only adds or changes rules for that folder.

Does .htaccess work on every server?

No, it is specific to Apache (and compatible servers). Nginx, for instance, does not read it: on a VPS running Nginx, the same rules go into the server configuration.

Sources

All posts

A question about your infrastructure?

Tell us about your project: an engineer answers and suggests the offer that fits it.