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? #
Apache’s own .htaccess tutorial 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 says it edits this file so that pretty permalinks work, inside a block marked BEGIN WordPress and END WordPress.
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 care about most:
- 301 redirects from old addresses to new ones;
- forcing one version of the site: HTTPS, with or without www;
- custom 404 pages and 410 Gone answers;
- X-Robots-Tag headers to keep PDFs or images out of the index;
- cache and compression rules that help page speed.
Google’s redirects guide 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 address.
Why does it matter for AI answers? #
AI crawlers obey the same server answers. A rule that blocks a user agent, a wrong redirect or a broken header in .htaccess affects GPTBot, ClaudeBot and PerplexityBot as much as Googlebot. Some owners block AI bots here by user agent name. That is a firmer lock than robots.txt, but user agents can be faked, so it is not proof of who is visiting. See block AI crawlers and verified bots.
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 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? #
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 says a Redirect with no status is temporary (302), and the keyword permanent, or the number 301, makes it permanent. Always write the status so you never ship a 302 by accident.
Common mistakes #
- Forgetting the status. A bare
Redirectsends a 302, which Google treats as temporary. - Redirect loops and chains. Two rules that send traffic round in a circle, or through 3 hops. See 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 and scripts. Google needs them to render the page; see rendering.
- Assuming it works everywhere. If the server is not Apache-compatible, or
AllowOverrideisNone, the file is ignored. - Leaving it world-readable with secrets. Never store keys or private paths in it.
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 and saves crawl budget.
How to check it #
- Test old addresses in our Redirect Checker: you want one hop and a 301 or 308.
- See the status code and headers, including X-Robots-Tag, in our HTTP Header Checker.
- Draft rules safely with the redirect generator and map many pages with the redirect mapper. Test patterns in the regex tester.
- After any change, scan for dead links with our Broken Link Checker, and make sure the sitemap only lists final addresses with our Sitemap Checker.
- Check that a bot rule does what you meant with the AI Bot Checker, and check security headers with the Security Headers Checker.
- In Search Console, watch the page with redirect list. In URSY Pro, the Redirect monitor alerts you when a rule breaks.
Questions #
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? #
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? #
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? #
No. robots.txt asks crawlers not to fetch. .htaccess can refuse requests or send headers. They do different jobs.
Related words #
Sources #
- Apache HTTP Server documentation: .htaccess files, checked 24 November 2026
- Apache HTTP Server documentation: core features (AllowOverride, ErrorDocument), checked 24 November 2026
- Apache HTTP Server documentation: mod_alias (Redirect, RedirectMatch), checked 24 November 2026
- WordPress Advanced Administration Handbook: Apache HTTPD / .htaccess, checked 24 November 2026
- Google Search Central: Redirects and Google Search, checked 24 November 2026
Updated 24 November 2026 by URSY.
Cite this page: .htaccess file. URSY. Updated 24 November 2026. https://ursy.com/wiki/htaccess-file/ · Section links
Tools for this
- LiveAI Agent ReadinessOpenAPI, .well-known files, MCP, agent instructionsRun the check →
- LiveAI Readiness Checkeris your site ready for ChatGPT, Gemini, Claude? Score + fixesRun the check →
- LiveAI Bot Checkerwhich AI bots your robots.txt lets inRun the check →
- LiveRobots.txt Checker + Generatortest any address against your rules, or write new onesRun the check →
- LiveSchema Markup CheckerRun the check →
- ProLog file analyserUpload or connect logs: every bot, every pageIn URSY Pro →
- ProBusiness Profile insightsCalls, directions, viewsIn URSY Pro →
- ProImage AI sidecars + licenceEach picture with a description file for AI and licence markupIn URSY Pro →