.htaccess is a plain text file of configuration rules for the Apache web server. You put it in a folder of your site, and its rules apply to that folder and every folder below it. The name starts with a dot, which hides it on Linux and macOS, so turn on hidden files in your FTP client or file manager to see it.
The point of the file is that you can change how the server behaves without access to its main configuration. That's why it's everywhere on shared hosting, and why content management systems use it. WordPress, for example, writes its own block of rules into .htaccess to make clean permalinks work.
Does your site use it?
Only if three things are true:
- The server runs Apache (or a server built to read Apache's files). nginx has no
.htaccess; its rules live in the server configuration. - The host allows it. Apache ignores
.htaccessfiles completely unless theAllowOverridesetting permits them, and that setting is off by default. Shared hosts usually turn it on. - The rule is allowed there. Each directive's documentation says whether it works in
.htaccess.
A quick test from Apache's own guide: put a nonsense word in the file and reload a page. If you don't get a 500 error, the server isn't reading the file.
What people use it for
These are the jobs that matter most for SEO. Each snippet goes into the .htaccess file in your site's root folder.
Redirect a page that moved
When a URL changes for good, Google recommends a permanent, server-side redirect. Google Search Central
Redirect permanent "/old-page.html" "https://www.example.com/new-page/"
Write permanent (or 301). Without it, Apache sends a temporary 302 redirect. Apache HTTP Server Also note that Redirect matches the start of the path: a rule for /blog also redirects /blog/any-post, adding the rest of the path to the new address.
Send every visitor to HTTPS
RewriteEngine On
RewriteCond "%{HTTPS}" "!=on"
RewriteRule "^" "https://%{HTTP_HOST}%{REQUEST_URI}" [R=301,L]
This uses mod_rewrite. If your site sits behind a load balancer or a service that handles the certificate, Apache sees every request as plain HTTP, and this rule loops. In that case, check the X-Forwarded-Proto header instead, or ask your host.
Keep files out of Google's index
PDFs and other files have no HTML <head> for a robots meta tag, so you send an X-Robots-Tag header instead. This example is from Google's documentation and needs Apache's headers module:
<Files ~ "\.pdf$">
Header set X-Robots-Tag "noindex, nofollow"
</Files>
Show your own 404 page
ErrorDocument 404 /not-found.html
Use a path that starts with a slash, not a full URL. With a full URL, Apache redirects to the error page, and visitors and search engines get a redirect instead of a 404 status for a page that doesn't exist. Apache HTTP Server See 404 error for what to put on the page.
Other common jobs
Password-protecting a folder, blocking image hotlinking, setting cache headers and adding security headers all work the same way: a few directives in the folder's .htaccess.
Before you edit it
- Keep a copy. One typo can turn every page in the folder into a 500 error. Save the working version first, change one thing, and reload the site.
- Check the result, not just the page. A redirect can look right in the browser and still send a 302. Check the HTTP status code of a few old and new URLs.
- Mind your CMS's block. When WordPress updates its rules, it replaces everything between its markers (
# BEGIN WordPress…# END WordPress) and keeps what's outside them; plugins use markers of their own the same way. WordPress Developer Resources Put your rules outside the markers, or they'll be overwritten. - Rewrite paths differ here. In
.htaccess,RewriteRulesees the path without its leading slash, relative to the folder. A rule copied from a server configuration may not match until you adjust it.
When not to use it
If you can edit Apache's main configuration, put the rules there. Apache's documentation recommends it for two reasons. Speed: when .htaccess is enabled, Apache checks every folder on the path for one, and reads it again on every request, even if the file doesn't exist. Control: anyone who can edit the file can change how the server behaves. Apache HTTP Server
On shared hosting, you usually can't, and .htaccess is the tool you have.