Preferences

Privacy is important to us, so you have the option of disabling certain types of storage that may not be necessary for the basic functioning of the website. Blocking categories may impact your experience on the website. More information (Cookie Policy)

.htaccess File: What It Is, Where It Lives and How to Use It

An .htaccess file configures Apache folder by folder. Learn where it is, how to write redirects, HTTPS and error rules, and the mistakes that hurt SEO.

Sorank customerSorank customerThomas Girauc, Sorank customerCelio Fabiano, Sorank customerSorank customerXavier Breull, Sorank customer
+ 9'000 Subscribers
.htaccess File: What It Is, Where It Lives and How to Use It
Notification: your site reached 20K clicks from Google Search in 28 days

Tools

On this page

Summary

An .htaccess file is a plain-text configuration file that the Apache web server reads in a directory to change its behavior for that folder and its subfolders, without editing the main server configuration. Site owners use it for redirects, HTTPS enforcement, custom error pages, access restrictions and caching rules.

Want to go further? Ask

The .htaccess file (short for "hypertext access") is how you give Apache instructions when you cannot touch the server itself. On shared hosting, it is often the only lever you have: you drop a few lines in a text file, upload it, and Apache applies them on the next request. For SEO, it is where most redirects, HTTPS rules and canonical host rules live on Apache sites.

What is an .htaccess file used for?

An .htaccess file is used to change how Apache serves the files of one directory. MDN describes it as a way to "configure directories of the web server they control without modifying the main configuration file" (MDN Web Docs, Apache configuration: .htaccess). The most common jobs are:

  • Redirects: send an old URL to a new one with a permanent 301 redirect or a temporary 302.
  • HTTPS and host rules: force the secure version of the site and pick one host, as explained in choosing between www and non-www.
  • Custom error pages: show your own 404 or 500 page instead of the server default.
  • Access control: protect a folder with a password, or block an IP address.
  • URL rewriting: turn /index.php?p=42 into a clean address. WordPress uses this for its permalinks.
  • Headers, compression and caching: set security headers, compress text files and tell browsers how long to keep them.

Where is the .htaccess file located?

The main .htaccess file sits in the root folder of your site, usually public_html or www, next to index.php or index.html. Its rules apply to that folder and to every subfolder below it. You can add another .htaccess file in a subfolder, for example /wp-admin/, and its rules apply only there.

The name starts with a dot, so most file managers and FTP clients hide it. Turn on "show hidden files" (in cPanel File Manager, it is in Settings) to see it. If there is no file, you can create one: a plain-text file named exactly .htaccess, with no extension.

MDN Web Docs page explaining Apache .htaccess configuration, with directive examples
Screenshot of developer.mozilla.org

How does Apache read the .htaccess file?

Apache reads .htaccess files at request time, not at startup. Each time a visitor or a bot asks for a page, Apache looks for an .htaccess file in the requested folder and in each parent folder, then applies the rules it finds. That is why a change works instantly, with no restart.

It is also why the file has a cost. MDN notes that "using .htaccess files slows down Apache", and recommends putting the same logic in the main configuration file (usually httpd.conf) inside a Directory block when you have access to it. The server administrator also decides what an .htaccess file may change, with the AllowOverride setting. If your rules seem ignored, that setting is the first thing to ask your host about.

.htaccess fileMain server configuration
Who can edit itAnyone with FTP or file manager accessThe server administrator
When changes applyOn the next requestAfter a server reload
ScopeOne folder and its subfoldersThe whole server or any block you define
SpeedRead on every requestRead once at startup
Typical useShared hosting, CMS rulesVPS, dedicated servers

Does every server use .htaccess?

No. The .htaccess file is an Apache feature, and servers compatible with it (such as LiteSpeed) read it too. Nginx does not: its rules go into the server configuration. Managed platforms like Shopify, Wix or Vercel do not give you an .htaccess file either; you set redirects in their dashboard or config file. Before you edit anything, check which server you run, for example with the free HTTP headers checker, which shows the Server response header when the host sends it.

How do you write a redirect in .htaccess?

For a single page, use the Redirect directive from Apache's mod_alias module. Google Search Central gives these two lines as the simplest form of server-side redirect (Google Search Central, Redirects and Google Search):

Redirect permanent "/old" "https://example.com/new"
Redirect temp "/two-old" "https://example.com/two-new"

The first is permanent (301), the second temporary (302). The same page explains why the choice matters: Google treats a permanent redirect as a signal that the target should be the canonical URL and shows the target in results, while a temporary redirect keeps the source page in results. Our complete guide to 301, 302, 307 and 308 redirects covers each case.

For patterns, use mod_rewrite. This rule from MDN sends every HTTP request to its HTTPS version:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

The [R] flag alone returns a 302. For a permanent move to HTTPS, write [R=301,L]. You also need a valid SSL certificate on the domain before you force HTTPS, or visitors land on a browser warning.

Tip

You have dozens of old URLs to move? Paste the old and new addresses into Sorank's free redirect generator: it writes the Apache rules for your .htaccess and flags loops and chains before you go live.

What else can you do with an .htaccess file?

Beyond redirects, four snippets cover most needs. Each one goes in the .htaccess file of the folder it should apply to.

Custom error pages

The ErrorDocument directive tells Apache which page to show for an error code:

ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html

Use a local path, not a full https:// address: with a full URL, Apache redirects the visitor and the error page no longer returns the real error code.

Password protection

To lock a staging folder, point Apache to a password file stored outside the public folder:

AuthType Basic
AuthName "Restricted area"
AuthUserFile /home/user/.htpasswd
Require valid-user

Blocking access

On Apache 2.4, Require controls who gets in. Require all denied blocks a whole folder; Require not ip 203.0.113.5 inside a RequireAll block bans one address.

Compression and caching

MDN's example compresses text responses with AddOutputFilterByType DEFLATE and sets browser cache lifetimes with mod_expires, for example ExpiresByType text/css "access plus 1 year". Both help page speed, as long as the modules are enabled on your server.

What does the WordPress .htaccess file contain?

WordPress writes its own block into the .htaccess file to handle pretty permalinks. The WordPress developer documentation calls the file "a distributed configuration file" and publishes the default block to restore a corrupted one (WordPress Developer Resources, Apache HTTPD / .htaccess):

# BEGIN WordPress
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

In plain words: if the requested file or folder does not exist, WordPress's index.php handles the request. WordPress may rewrite everything between the BEGIN and END markers when you save permalinks, so put your own rules above that block, never inside it.

WordPress developer documentation page showing the default .htaccess rules for permalinks
Screenshot of developer.wordpress.org

Common .htaccess file mistakes

One wrong character in .htaccess can take the whole site down. These are the mistakes that hurt SEO most often.

  1. A syntax error that returns 500 on every page. You spot it right after saving: the site shows "Internal Server Error". Fix it by restoring your backup copy, then check the line you changed. A 5xx server error that lasts makes Google slow down its crawl.
  2. Redirect loops and chains. An HTTPS rule and a www rule that send each other back and forth create a loop; stacked rules create chains of several hops. Spot them by testing a few old URLs in the redirect chain checker. Fix them by sending every old URL straight to its final address in one hop.
  3. Using 302 for a permanent move. [R] without a code, or Redirect temp, keeps the old page in Google's results. Spot it in the checker's status codes. Fix it with [R=301,L] or Redirect permanent.
  4. Blocking bots by mistake. A user-agent or IP rule copied from a forum can block Googlebot or the AI crawlers that feed ChatGPT and Claude. Spot it in your server logs (403 responses to bots). Fix it by removing the rule, and use the robots.txt file to manage crawling instead.
  5. Editing a file the server ignores. On Nginx, or when AllowOverride is set to None, nothing happens. Spot it when a test redirect does not fire. Fix it in the server configuration or the host's dashboard.
  6. Rules in the wrong order. Apache's mod_rewrite reads rules top to bottom, and the [L] flag stops the run. A specific redirect placed after the WordPress block never fires. Move it above.

Warning

Download a copy of your .htaccess file before every edit. If the site breaks, uploading the copy restores it in seconds.

Why does the .htaccess file matter for SEO and GEO?

On an Apache site, the .htaccess file decides what search engines and AI crawlers receive when they request a URL. Clean 301 redirects pass old URLs to their new address, which is what Google uses to pick the canonical version during a redesign or a move to HTTPS (see our HTTPS and SEO migration guide). Fast, single-hop responses save crawl budget on large sites. And a stray blocking rule can hide your pages from the AI assistants that answer your customers' questions, even when your content is the best on the topic.

Sorank's GEO optimization tracks whether ChatGPT, Claude and Gemini recommend your brand, so a sudden drop after a server change shows up in the same place as your Google traffic.

Conclusion

The .htaccess file is Apache's per-folder settings file: redirects, HTTPS, error pages, access rules and caching, applied on the next request without a restart. Keep it short, back it up before each edit, use 301 for permanent moves, and test every change with a redirect checker. If you run Nginx or a hosted platform, the same rules exist, but they live in another place.

Sources

Frequently asked questions

How do I find the .htaccess file on my site?

Look in the root folder of your site, usually public_html or www, next to index.php. The name starts with a dot, so turn on the option to show hidden files in your file manager or FTP client. If it is not there, you can create a plain-text file named .htaccess with no extension.

Can a mistake in .htaccess break my website?

Yes. A single syntax error usually makes Apache return a 500 Internal Server Error on every page of the folder. Keep a copy of the file before each edit, and upload it again if the site breaks.

Does Nginx use an .htaccess file?

No. The .htaccess file is an Apache feature, also read by compatible servers such as LiteSpeed. On Nginx, redirects and access rules go into the server configuration, and on hosted platforms they are set in the dashboard.

Our Blog for Ambitious Company

Testimonials from ambitious founders

0 to 10000 visitors in 5 months

Celio Fabianomonrobotlavevitre.frGoogleE-commerce

0 to 5000 visits in 8 months

Thibault BMsorank.comGoogleSoftware

0 to 1104 clicks in 4 months

Dominiqueflight-sensations-idf.comGoogleLeisure and aviation

0 to 163 clicks in 6 months

Coworking Pariscoworking-paris.frGoogleOffice broker

x92 clicks in 7 months

Guide Zanzibarguidezanzibar.comGoogleTravel