---
title: ".htaccess file"
description: "An .htaccess file sets Apache rules for one folder and below: redirects, HTTPS, error pages and headers. How it works, examples and mistakes."
url: https://ursy.com/wiki/htaccess-file/
updated: 2026-11-24T22:19:04+00:00
publisher: URSY (https://ursy.com/)
cite: Name URSY and link https://ursy.com/wiki/htaccess-file/
---

# .htaccess file

> An .htaccess file sets Apache rules for one folder and below: redirects, HTTPS, error pages and headers. How it works, examples and mistakes.

**An .htaccess file is a plain text file that the Apache web server reads to apply settings to one folder of a website and every folder below it, such as redirects, access rules, custom error pages and headers.** Apache calls these “distributed configuration files”. The dot at the start of the name hides the file on most systems. It only works where the server runs Apache, or software that copies Apache’s rules, and where the owner has turned it on.

## What is an .htaccess file? {#what-is-an-htaccess-file}

Apache’s own [.htaccess tutorial](https://httpd.apache.org/docs/current/howto/htaccess.html) explains it: you place a file of directives in a folder, and the directives apply to that folder and all its subfolders. The file in the site root therefore affects the whole site. A file deeper down can add or override rules for just that part.

The server owner can rename it with the `AccessFileName` directive, but almost everyone keeps the default name. WordPress is a common case: its [Apache guide](https://developer.wordpress.org/advanced-administration/server/web-server/httpd/) says it edits this file so that pretty [permalinks](https://ursy.com/wiki/permalink/) work, inside a block marked BEGIN WordPress and END WordPress.

## Why does it matter for search? {#why-does-it-matter-for-search}

Many site owners cannot edit the main server settings. The .htaccess file is often their only way to control how the server answers. That makes it the usual home of rules that [search engines](https://ursy.com/wiki/search-engine/) care about most:

- [301 redirects](https://ursy.com/wiki/301-vs-302-redirect/) from old addresses to new ones;
- forcing one version of the site: HTTPS, with or without www;
- custom 404 pages and [410 Gone](https://ursy.com/wiki/404-and-410/) answers;
- [X-Robots-Tag](https://ursy.com/wiki/x-robots-tag/) headers to keep PDFs or images out of the index;
- cache and compression rules that help [page speed](https://ursy.com/wiki/page-speed/).

[Google](https://ursy.com/wiki/google/)’s [redirects guide](https://developers.google.com/search/docs/crawling-indexing/301-redirects) names the .htaccess file on Apache as one place to set server-side redirects. It says server-side redirects have the best chance of being read correctly, and that permanent redirects tell its indexing that the target should be the [canonical](https://ursy.com/wiki/canonical-tag/) address.

## Why does it matter for AI answers? {#why-does-it-matter-for-ai-answers}

[AI crawlers](https://ursy.com/wiki/ai-crawler/) obey the same server answers. A rule that blocks a [user agent](https://ursy.com/wiki/user-agent/), a wrong redirect or a broken header in .htaccess affects [GPTBot](https://ursy.com/wiki/gptbot/), [ClaudeBot](https://ursy.com/wiki/claudebot/) and [PerplexityBot](https://ursy.com/wiki/perplexitybot/) as much as [Googlebot](https://ursy.com/wiki/googlebot/). Some owners block AI [bots](https://ursy.com/wiki/bot-web-robot/) here by user agent name. That is a firmer lock than [robots.txt](https://ursy.com/wiki/robots-txt/), but user agents can be faked, so it is not proof of who is visiting. See [block AI crawlers](https://ursy.com/wiki/block-ai-crawlers/) and [verified bots](https://ursy.com/wiki/verified-bots/).

## How does it work? {#how-does-it-work}

Each time a page is asked for, Apache walks the folder path and reads every .htaccess file it finds, then applies the rules in order. Apache’s tutorial warns that this costs speed. When .htaccess is allowed, the server looks for the file in every folder on each request, whether or not one exists. Rewrite patterns in .htaccess are also recompiled each time, while in the main settings they are compiled once.

The server owner decides what the file may do through `AllowOverride`. Apache’s [core documentation](https://httpd.apache.org/docs/current/mod/core.html) gives the default as `None`, which means .htaccess files are ignored entirely unless someone switches them on. That is why the same file can work on one server and do nothing on another.

## What does a typical file look like? {#what-does-a-typical-file-look-like}

A short example for a site that has moved two pages and wants HTTPS everywhere:

```
# Send old pages to new ones (permanent)
Redirect 301 /old-prices.html /prices/
RedirectMatch 301 ^/blog/2019/(.*)$ /news/$1

# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Friendly error page
ErrorDocument 404 /not-found/

# Keep PDFs out of search results
<Files "*.pdf">
  Header set X-Robots-Tag "noindex"
</Files>
```

Apache’s [mod_alias documentation](https://httpd.apache.org/docs/current/mod/mod_alias.html) says a `Redirect` with no status is temporary (302), and the [keyword](https://ursy.com/wiki/keyword/) `permanent`, or the number 301, makes it permanent. Always write the status so you never ship a 302 by accident.

## Common mistakes {#common-mistakes}

- **Forgetting the status.**  A bare `Redirect` sends a 302, which Google treats as temporary.
- **[Redirect loops](https://ursy.com/wiki/redirect-loop/) and chains.**  Two rules that send traffic round in a circle, or through 3 hops. See [redirect chain](https://ursy.com/wiki/redirect-chain/).
- **One typo takes the site down.**  A broken directive usually gives every visitor a 500 error. Keep a copy before you edit.
- **Rules in the wrong order.**  A broad rewrite above a specific redirect can swallow it.
- **Blocking [CSS](https://ursy.com/wiki/css/) and scripts.**  Google needs them to render the page; see [rendering](https://ursy.com/wiki/rendering/).
- **Assuming it works everywhere.**  If the server is not Apache-compatible, or `AllowOverride` is `None`, the file is ignored.
- **Leaving it world-readable with secrets.**  Never store keys or private paths in it.

## A worked example {#a-worked-example}

A UK florist moves from `http://www.example.co.uk/shop/roses.html` to `https://example.co.uk/roses/`.

| Request | Bad setup | Good setup |
| --- | --- | --- |
| http://www…roses.html | 302 to https, then 302 to non-www, then 301 to /roses/ | One 301 straight to https://example.co.uk/roses/ |
| https://example.co.uk/roses | 404 | 301 to the version with a slash |
| /old-offer/ (gone for good) | Redirect to home page | 410 Gone |

The good setup gives crawlers one hop and a clear answer, which keeps [link equity](https://ursy.com/wiki/link-equity/) and saves [crawl budget](https://ursy.com/wiki/crawl-budget/).

## How to check it {#how-to-check-it}

1. Test old addresses in our [Redirect Checker](https://ursy.com/redirect-checker/): you want one hop and a 301 or 308.
2. See the status code and headers, including X-Robots-Tag, in our [HTTP Header Checker](https://ursy.com/http-header-checker/).
3. Draft rules safely with the [redirect generator](https://ursy.com/webmaster-tools/redirect-generator/) and map many pages with the [redirect mapper](https://ursy.com/webmaster-tools/redirect-mapper/). Test patterns in the [regex tester](https://ursy.com/webmaster-tools/regex-tester/).
4. After any change, scan for dead links with our [Broken Link Checker](https://ursy.com/broken-link-checker/), and make sure the sitemap only lists final addresses with our [Sitemap Checker](https://ursy.com/sitemap-checker/).
5. Check that a bot rule does what you meant with the [AI Bot Checker](https://ursy.com/ai-bot-checker/), and check security headers with the [Security Headers Checker](https://ursy.com/security-headers-checker/).
6. In [Search Console](https://ursy.com/wiki/google-search-console/), watch the [page with redirect](https://ursy.com/wiki/page-with-redirect/) list. In URSY Pro, the [Redirect monitor](https://ursy.com/pro/redirect-monitor/) alerts you when a rule breaks.

## Questions {#questions}

### Where is my .htaccess file? {#where-is-my-htaccess-file}

Usually in the site’s root folder, next to the home page files. Turn on “show hidden files” in your file manager to see it.

### Do changes need a server restart? {#do-changes-need-a-server-restart}

No. Apache reads the file on every request, so edits apply to the next page load.

### Should I use .htaccess if I can edit the main settings? {#should-i-use-htaccess-if-i-can-edit-the-main-settings}

Apache says no: put the rules in the main server settings, where they load once at start-up and run faster.

### Can .htaccess replace robots.txt? {#can-htaccess-replace-robots-txt}

No. robots.txt asks crawlers not to fetch. .htaccess can refuse requests or send headers. They do different jobs.

## Related words {#related-words}

- [Redirect](https://ursy.com/wiki/redirect/)
- [HTTP status code](https://ursy.com/wiki/http-status-code/)
- [Technical SEO](https://ursy.com/wiki/technical-seo/)
- [Move a website without losing traffic](https://ursy.com/wiki/move-a-website-without-losing-traffic/)
- [Origin server](https://ursy.com/wiki/origin-server/)

## Sources {#sources}

- [Apache HTTP Server documentation: .htaccess files](https://httpd.apache.org/docs/current/howto/htaccess.html), checked 24 November 2026
- [Apache HTTP Server documentation: core features (AllowOverride, ErrorDocument)](https://httpd.apache.org/docs/current/mod/core.html), checked 24 November 2026
- [Apache HTTP Server documentation: mod_alias (Redirect, RedirectMatch)](https://httpd.apache.org/docs/current/mod/mod_alias.html), checked 24 November 2026
- [WordPress Advanced Administration Handbook: Apache HTTPD / .htaccess](https://developer.wordpress.org/advanced-administration/server/web-server/httpd/), checked 24 November 2026
- [Google Search Central: Redirects and Google Search](https://developers.google.com/search/docs/crawling-indexing/301-redirects), checked 24 November 2026

*Updated 24 November 2026 by URSY.*
