What is .htaccess? A practical guide

What .htaccess files do, where they apply, the rules people use most, and how to recover quickly when one breaks your site.

ExplainerIntermediate2 min readUpdated

An .htaccess file gives the web server instructions for the folder it’s in, and every folder below. It’s how you set redirects, block access and change caching without server-wide access.

Where it lives

Usually public_html/.htaccess. A subfolder can have its own, adding to or overriding the parent’s rules. It’s a hidden file. How to show hidden files like .htaccess

What people use it for

JobGuide
Force HTTPSHow to force HTTPS (redirect HTTP to HTTPS)
Redirect pages or domainsRedirects with .htaccess: copy-ready examples
Custom 404 pagesCreating custom 404 and error pages
Security headersAdding security headers with .htaccess
Block access to filesStoring configuration and secrets outside public_html
Password protectionHow to password-protect a folder
Clean URLs for appsWritten by WordPress and frameworks

WordPress’s block

WordPress manages a section between # BEGIN WordPress and # END WordPress. Put your own rules above it; WordPress rewrites the inside of its block when you save permalinks.

The golden rule: one mistake breaks everything

A typo in .htaccess returns a 500 Internal Server Error for the whole folder immediately.

  1. Copy the file before editing (.htaccess.bak).
  2. Make one change at a time.
  3. Reload the site straight after saving.
  4. If you get a 500, restore the copy.

Diagnosing a broken .htaccess

Rename it to .htaccess-off. If the site returns, the file was the cause. The domain’s error log names the problem line, such as “Invalid command” or “RewriteRule: bad flag”. Viewing website statistics and logs in DirectAdmin

Performance

The server reads .htaccess files on every request. Keep them tidy: remove rules from plugins you no longer use.

Traxio accounts include SSH, Git, cron and PHP 7.4 to 8.3. Try the PHP hosting is free for 30 days with no card, then £0.99 a month.

Popular

Tip: press / to search from any pageSee all results