On Apache servers, the .htaccess file is a small but powerful configuration file that controls redirects, HTTPS, caching and more, several of which directly affect SEO and page speed. Using .htaccess for SEO lets you fix canonicalisation, enforce security and speed up delivery without touching application code. This guide covers the highest-value rules and, just as important, the safety habits that keep one typo from taking your site down.
What .htaccess does
An .htaccess file sits in a directory and applies Apache configuration to that folder and everything below it. Because it is read on every request, it is the natural place to handle server-level SEO tasks: forcing a single canonical domain, redirecting old URLs, requiring HTTPS, and setting the caching and compression headers that make pages load faster. If your host runs Nginx instead of Apache, the same ideas apply through the server config rather than an .htaccess file.
Force HTTPS and one canonical domain
The most important SEO job for .htaccess is making sure every visitor and crawler lands on one version of your site. A page reachable at both http and https, or at both www and non-www, is effectively duplicate content that splits your signals. Two rules fix this: redirect all HTTP traffic to HTTPS, and redirect either www to non-www or vice versa (pick one and be consistent). Both should be 301 permanent redirects so authority consolidates on the canonical version.
Set up 301 redirects
.htaccess is where many sites manage 301 redirects for changed or retired URLs. Whenever you change a slug, restructure folders, or remove a page, a 301 from the old URL to the best replacement preserves rankings and stops visitors hitting a 404. The two things to watch: avoid redirect chains (send the old URL straight to the final destination in one hop), and do not lazily redirect everything to the homepage, point each old URL at its closest match.
Speed: caching and compression
Two .htaccess tweaks give an easy performance win, which supports Core Web Vitals:
| Rule | Effect |
|---|---|
| Browser caching (Expires headers) | Returning visitors reuse cached CSS, JS and images instead of re-downloading |
| Gzip / Brotli compression | Text files (HTML, CSS, JS) are sent smaller, so pages load faster |
These do not change what users see, they just make delivery faster, which improves the experience and the page-speed signals Google measures.
The golden safety rule: back it up first
Here is the warning that matters most: a single syntax error in .htaccess can bring down your entire site with a 500 error, because Apache reads the file on every request. Before editing, always save a copy of the working file. Make one change at a time, test the site immediately after, and if anything breaks, restore the backup. Never paste a large block of rules you do not understand into a live site’s .htaccess without a rollback ready.
Test after every edit
Because .htaccess is read on every request, the feedback loop after a change is instant, and so is the damage if you get it wrong. Adopt a simple discipline: after any edit, immediately load the site in a fresh browser tab and check a few key pages, including one that should redirect and one that should not. Confirm HTTPS is enforced, the canonical domain resolves correctly, and no page returns a 500 error. Spot-check a redirect end to end: the old URL should land on the right new page in a single hop. If anything is off, restore your backup before diagnosing, a broken .htaccess takes the whole site with it, so a fast rollback is more important than a fast fix.
Generate a safe .htaccess
Because the stakes are high and the syntax is unforgiving, generating rules beats writing them from memory. Our free .htaccess generator builds a safe Apache configuration for HTTPS, canonical domain, redirects, caching, compression and basic security, so you get correct, tested-pattern rules to paste in. For the redirect rules specifically, the 301 redirect generator turns a list of old and new URLs into ready-made directives.
The takeaway
The .htaccess file is where Apache sites enforce HTTPS and a single canonical domain, manage 301 redirects, and enable caching and compression, all of which help SEO and speed. Its power is matched by its risk: one typo can take the site down, so always back up, change one thing at a time, and test. Build safe rules with the .htaccess generator, and explore the rest of our free SEO tools.