Skip to content
RIPE NCC member · Local Internet RegistryCall us on 020 3318 3449sales@hosting-telecom.uk

The .htaccess file: home page, errors, redirects

How to use .htaccess to choose the home page, set up your own error pages, hide the file listing and configure redirects.

Contents

.htaccess is a file of site settings for the web server. It lives in the public_html folder (or in any subfolder) and applies to that folder and everything inside it. It works on our hosting, and redirects (mod_rewrite) are enabled — you don’t need to switch anything on. Much of what this article covers can also be done without the file, with a few clicks in cPanel — the relevant sections are mentioned below.

Important One typo in .htaccess and the whole site shows “500 Internal Server Error”. Save a copy of the file before you edit it. If the site stops opening, put the copy back.

The wrong home page opens

Uploaded your site, but an old placeholder opens? Most likely an old index.html is still in the folder, while your site starts with index.php. Delete the extra file, or tell the server in .htaccess which file to open first:

DirectoryIndex index.php index.html

Your own error pages

Add these lines to .htaccess:

ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

Write the path from the root of the site, without https:// and the domain. If you give a full address, the server sends the visitor to another page instead of showing an error — and search engines won’t realise the page doesn’t exist. Without the file, you can do the same in cPanel, in the Error Pages section.

File listing of a folder

If a folder has no home page, the server may show everyone a list of its files. To prevent this, add to .htaccess:

Options -Indexes

In cPanel, use the Indexes section.

Redirects

The two most common redirects are also just a few lines in .htaccess:

# redirect one page to another
Redirect 301 /old-page/ https://example.co.uk/new-page/

# redirect www to the address without www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

To make the site open only over HTTPS, see Redirecting to HTTPS. Without the file, you can set up redirects in cPanel, in the Redirects section.

Restricting access to a site or folder

# allow access from one address only — for example, while the site is under development
Require ip 203.0.113.10

You can also block individual addresses in cPanel, in the IP Blocker section.

Character encoding

If the browser gets the encoding of your pages wrong, tell it which to use: AddDefaultCharset UTF-8. For more detail, see Question marks and garbled characters instead of text.

Didn’t find the answer? Ask our support team — we’ll help.

Choose which cookies we may use. You can change your mind at any time with the “Cookie settings” link at the bottom of every page.

Necessary

Always active

They keep the site secure and remember your cookie choice. They don’t identify you.

Google Analytics counts visits and shows which pages people find useful, so we can improve the site. Which cookies