What is .htaccess?
.htaccess is a plain text file in your website’s folder that tells an Apache or LiteSpeed web server how to behave: where to redirect visitors, which files to protect, how long browsers may keep images, and more. Most shared hosting — cPanel, Hostinger’s hPanel, Namecheap and others — reads it, and changes take effect as soon as you save it.
How to add it to your site
- Tick the options you need and add any redirects.
- Copy the result. In your hosting File Manager, open the public_html folder, show hidden files, and edit .htaccess (or create it).
- Paste and save, then open your site in a private window to check that everything loads. If anything breaks, restore the old file.
What the options do
- Always use https and www: sends every visitor to one address in a single redirect, which is better for speed and SEO than two redirects in a row.
- Behind Cloudflare or a CDN: such services connect to your server over http, so the rule checks the visitor’s original protocol instead — otherwise the redirect would loop.
- Hide .html: shows /about instead of /about.html and redirects the old addresses.
- Caching and compression: make repeat visits much faster and pages smaller.
- Security headers and blocked files: stop common attacks, and keep backups, logs and config files from being downloaded.
Good to know
- Keep a copy of your existing .htaccess before replacing it. WordPress and other apps add their own rules — paste these above the “# BEGIN WordPress” block rather than deleting it.
- Nginx servers do not read .htaccess; their rules go in the server configuration instead.
- Turn on HSTS only once https works everywhere on your site: browsers remember it for a year.
Frequently asked questions
How do I redirect http to https with .htaccess?
Tick “Always use https”. The rule checks whether the connection is secure and, if not, sends a permanent (301) redirect to the same page on https.
Why does my site show “too many redirects” after adding the rule?
Usually because a CDN such as Cloudflare connects over http. Tick “My site is behind Cloudflare or a CDN”, or set Cloudflare’s SSL mode to Full.
Where is the .htaccess file?
In the main folder of your site, usually public_html. It starts with a dot, so turn on “show hidden files” in your File Manager or FTP program.