Skip to main content

An .htaccess file is a configuration file that lets you control certain aspects of how an Apache web server handles requests for your website. It can be used to create redirects, manage URL rewriting, restrict access to files and directories, configure custom error pages and apply other directory-level settings.

You may encounter this file when managing a website through cPanel, troubleshooting a WordPress site or changing how visitors reach particular pages. Used correctly, it is a practical way to manage website behaviour without editing the main Apache server configuration. Used incorrectly, however, a single invalid directive can make pages unavailable or trigger a 500 Internal Server Error.

This guide explains what an .htaccess file does, where to find it, how to edit it safely and which common rules are useful. The examples are intended for Apache-compatible hosting. They will not work unchanged on every web server or hosting configuration.

What Is an .htaccess File?

The name .htaccess is commonly understood as “hypertext access”. It is a plain-text, per-directory configuration file supported by the Apache HTTP Server. It allows permitted Apache directives to be applied to a directory and, depending on the directive and configuration, its subdirectories.

Unlike the main Apache configuration files, an .htaccess file can often be edited by a website owner who does not have administrator or root access to the server. This is one reason it is common on shared hosting accounts.

Apache does not automatically apply every directive placed in this file. The server administrator must allow the relevant overrides, and the directive must be valid in that context. If overrides are disabled, Apache may ignore the file.

Where Is the .htaccess File Located?

The file is usually in the document root of the website. On many cPanel accounts this is public_html, although addon domains and subdomains may have their own document roots. A WordPress installation commonly has an .htaccess file in the directory containing its wp-config.php file and main WordPress folders.

Because the filename begins with a dot, it is treated as a hidden file on many Unix-like systems. If you cannot see it, enable the option to show hidden files in your hosting File Manager or FTP client.

How to find it in cPanel

  1. Sign in to your hosting control panel and open File Manager.
  2. Open the website’s document root, often public_html.
  3. Use the File Manager settings to show hidden files (dotfiles).
  4. Look for .htaccess. If it does not exist, do not assume you must create one; first check whether your application or hosting provider manages the rules another way.

You can also locate the file using an FTP client or SSH, if your hosting plan provides those options. Always confirm that you are editing the correct domain’s document root.

How Does an .htaccess File Work?

When Apache handles a request, it can read applicable .htaccess files in the directory path. Directives may affect the requested resource and, where inherited, resources in lower-level directories. A more specific file in a subdirectory can add to or override some parent-directory behaviour, depending on the directive.

Whether Apache reads these files depends on its server configuration. The AllowOverride setting controls which classes of directives may be used in .htaccess files. With AllowOverride None, Apache does not use .htaccess files for that directory. Some hosting environments use AllowOverrideList to permit only selected directives. See the Apache guide to .htaccess files for the server-side details.

Changes usually take effect without restarting Apache because the file is checked during request processing. This convenience comes with a trade-off: complex rules can add overhead, and a syntax error or unsupported directive can cause an error response. If you administer the server, placing suitable rules in the main virtual-host configuration is generally more efficient than using .htaccess.

What Can an .htaccess File Do?

1. Redirect visitors to a new URL

Redirects send visitors and search engines from an old URL to a new one. A permanent redirect can be useful when a page has moved and the replacement should become the preferred destination.

Redirect 301 /old-page.html https://www.example.com/new-page/

This uses Apache’s mod_alias module. For simple redirects, it is often clearer than writing a rewrite rule. Check the destination carefully and avoid redirect chains or loops. Do not add a redirect simply because a page returns a genuine 404; first decide whether a relevant replacement exists.

2. Rewrite URLs with mod_rewrite

The mod_rewrite module can rewrite requests internally or redirect them externally based on conditions. Apache documents its directives and flags in the mod_rewrite reference. WordPress commonly uses rewrite rules to support readable permalink URLs.

RewriteEngine On
RewriteRule ^old-section/(.*)$ /new-section/$1 [R=301,L]

This example redirects matching paths from /old-section/ to /new-section/. The rule assumes it is placed in a suitable per-directory context and should be tested against the actual URL structure before use. The R=301 flag sends a permanent redirect, while L stops processing further rules in the current ruleset.

3. Protect a directory with a password

Apache can require a username and password before allowing access to a directory. This commonly uses an .htaccess file together with an .htpasswd file containing password hashes. Keep the password file outside the public document root wherever possible, and use HTTPS so credentials are not sent over an unencrypted connection.

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

Replace the example path with the actual absolute path supplied by your hosting provider. Create the password file using a trusted hosting tool or Apache’s supported password utility. Basic authentication is not a substitute for application-level security, and it should not be used over plain HTTP.

4. Restrict access to selected files or IP addresses

Apache 2.4 uses the Require authorisation directives, documented in the mod_authz_core reference. For example, to allow only a trusted IP address to access a protected directory, a rule may look like this:

Require ip 203.0.113.10

203.0.113.10 is an example address reserved for documentation. Replace it with the real trusted address. Be careful when restricting access: blocking your own IP or using an incorrect address can lock you out. Older examples using Deny from and Allow from belong to legacy access-control syntax and may require compatibility support on Apache 2.4.

For sensitive files, prefer to prevent public access at the server or application level rather than relying on a broad collection of manually maintained rules.

5. Display a custom error page

You can tell Apache to display a page you have created when a particular HTTP error occurs.

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html

The paths are relative to the website’s document root. Make sure the custom pages exist and can be accessed, otherwise visitors may receive another error. A custom 404 page should help visitors find useful content, but it should still return the correct 404 HTTP status for a missing resource.

For more detail, see our guide to configuring a custom error page on your hosting.

6. Redirect HTTP traffic to HTTPS

If your website has a working SSL/TLS certificate, you may use Apache rewrite rules to redirect HTTP requests to HTTPS. A common pattern is:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This is a general example, not a universal drop-in rule. If your site is behind a reverse proxy or load balancer that terminates TLS, the server may see the connection from the proxy as HTTP. In that situation, a rule based only on %{HTTPS} can create redirect loops. Use the hosting provider’s documented configuration and avoid adding a second redirect if your control panel or application already enforces HTTPS.

For a dedicated walkthrough, read how to force HTTPS redirects in .htaccess.

7. Enable browser caching

Apache’s mod_expires module can set expiry information for static resources such as images, stylesheets and scripts. This helps browsers reuse files instead of requesting them again on every visit.

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpeg "access plus 30 days"
  ExpiresByType image/png "access plus 30 days"
  ExpiresByType text/css "access plus 7 days"
  ExpiresByType application/javascript "access plus 7 days"
</IfModule>

Choose expiry periods that match how often your files change. If you use long caching periods, use versioned filenames or cache-busting techniques when you update assets. The rules have no effect if the required module is unavailable or disabled.

8. Configure compression

Where the server supports it, Apache’s mod_deflate can compress suitable text-based responses before sending them to browsers. Compression can reduce transfer sizes, but should be configured with the server administrator’s guidance and tested for compatibility.

See our separate guide on enabling Gzip compression in .htaccess for a focused explanation.

9. Prevent image hotlinking

Hotlink protection attempts to stop other websites from embedding files directly from your server. It can reduce unwanted bandwidth use, but referer headers are not always present and can be modified or omitted. Rules that block every request without an expected referer may also affect legitimate visitors, privacy tools or search crawlers.

If you enable hotlink protection, test image delivery from your own site, mobile browsers and any content delivery network you use. Many hosting control panels provide a hotlink protection interface that is easier to maintain.

10. Set MIME types and other file-handling rules

Apache can associate file extensions with media types so that browsers and applications handle files appropriately. For example, the AddType directive can define a media type for a particular extension when the server configuration permits it.

AddType image/webp .webp

Do not add MIME rules without checking whether Apache or your hosting environment already defines them. Incorrect content types can cause browsers to handle files unexpectedly.

How to Edit an .htaccess File Safely

  1. Make a backup. Download the current file before changing anything. Keep an untouched copy so you can restore it.
  2. Use a plain-text editor. Avoid word processors that may insert formatting characters or change quotation marks.
  3. Make one change at a time. This makes it easier to identify the rule responsible if something stops working.
  4. Check the server environment. Confirm that the directive and module are supported and permitted by the host.
  5. Save and test. Check the homepage, important pages, redirects, forms and any protected directories.
  6. Restore the backup if necessary. If the site returns an error, restore the known-good file using File Manager or FTP.

Do not change file permissions to make an .htaccess file executable. It is a text configuration file. Follow your hosting provider’s recommended ownership and permissions for website files.

Using .htaccess with WordPress

On Apache-based hosting, WordPress can use .htaccess rewrite rules to support pretty permalinks. WordPress may create or update its standard rewrite section when you save permalink settings, provided the server allows it and the file is writable.

If WordPress URLs suddenly return 404 errors, open the WordPress dashboard and visit Settings → Permalinks, then save the settings to refresh rewrite rules. Back up the file first if you plan to edit it manually. Do not replace the entire file with a generic example: plugins, security tools and hosting features may add their own rules.

Some WordPress installations run on Nginx, which does not read .htaccess files. In that case, permalink rules must be configured in the Nginx server configuration or managed by the hosting provider. A WordPress site can also be served by LiteSpeed, which supports many Apache-style rules, but behaviour depends on its configuration.

Common .htaccess Problems and How to Troubleshoot Them

500 Internal Server Error after editing

A syntax error, unsupported directive, missing module or disallowed override can cause a server error. Restore the backup first, then review the server error log and reintroduce changes one at a time. Our guide to fixing a 500 Internal Server Error explains the wider troubleshooting process.

Redirect loop

A loop can happen when multiple systems enforce HTTPS or when a reverse proxy makes Apache detect the wrong connection scheme. Remove duplicate redirect rules and follow the proxy or hosting provider’s instructions.

Rules appear to have no effect

Check that the website is running on Apache or a compatible server, that .htaccess overrides are enabled, that the required module is loaded and that the file is in the correct document root. Also check for a more specific .htaccess file in a subdirectory.

403 Forbidden after adding an access rule

Review the Require rules, the client IP address Apache sees, file and directory permissions, and any security module or firewall rules. Remove the new restriction temporarily if it blocks legitimate access.

WordPress pages return 404 errors

Refresh the permalink settings, check the WordPress rewrite section and confirm that the hosting server supports the rules being used. Do not assume every 404 requires a redirect; the cause may be a missing page or a routing configuration issue. See what a 404 error means and how to fix it for further guidance.

Does .htaccess Work on Every Web Server?

No. .htaccess is an Apache feature. Nginx does not process .htaccess files, and Microsoft IIS uses a different configuration system. LiteSpeed supports many Apache-compatible directives, but you should still confirm the exact features available on your hosting plan.

Even on Apache, some hosting providers disable overrides or restrict which directives customers can use. If a rule does not work, ask your provider whether the directive and module are supported rather than repeatedly changing the file.

Shared Hosting and .htaccess Support

For website owners who need to manage redirects, WordPress permalinks and other Apache-compatible settings, a hosting account with a suitable control panel and documented server support can make routine administration easier. Before choosing a plan, confirm which web server is used and whether the directives you need are enabled.


Shared web hosting plans with cPanel

Shared Hosting with cPanel

Explore hosting plans with cPanel, Apache-compatible website management, MySQL or MariaDB databases and backup features.

Explore Shared Hosting

Frequently Asked Questions

Can I delete my .htaccess file?

You can remove it, but doing so may break redirects, access restrictions, caching rules or WordPress permalinks. Back it up and identify what depends on it before deleting it.

Can I have more than one .htaccess file?

Yes. Apache can read .htaccess files in different directories when overrides are enabled. Rules in a subdirectory can affect requests within that directory. Multiple files can make troubleshooting more complicated, so keep them organised.

Does .htaccess affect website speed?

It can add a small amount of request-processing overhead because Apache checks applicable files while handling requests. Large or complex rulesets can increase that overhead. For server administrators, putting suitable rules in the main server configuration is generally more efficient.

Do I need .htaccess for WordPress?

Not always. WordPress commonly uses .htaccess for permalink rewriting on Apache, but Nginx uses server-level configuration instead. Your hosting environment determines which method applies.

Can I edit .htaccess from cPanel?

Yes, if your hosting provider includes File Manager and grants access to the website files. Enable hidden files to see it, make a backup and use a plain-text editor.

Final Thoughts

An .htaccess file gives website owners a useful way to manage selected Apache settings without editing the server’s main configuration. It can handle redirects, URL rewriting, access control, custom error pages, caching and other tasks, provided the server allows the required directives.

The safest approach is to understand what each rule does, back up the existing file, make one change at a time and test the result. If your server does not support .htaccess or a rule is ignored, check the hosting configuration before trying alternative directives.