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.
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.htmlYour own error pages
Add these lines to .htaccess:
ErrorDocument 404 /404.html
ErrorDocument 500 /500.htmlWrite 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 -IndexesIn 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.10You 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.