How to Edit the .htaccess File
The .htaccess file is a hidden Apache or LiteSpeed configuration file in your site's document root, usually public_html. Show hidden files in your file manager, copy it to .htaccess.bak, then add one rule at a time and test the site after each save. If every page returns 500, rename the file and restore the backup.
For site owners and developers on Apache or LiteSpeed hosting who need to add redirects, caching or security rules without downtime.
knowledge
- Read time
- 5 min
- Sections
- 7
- Updated
- 14 Nov 2026
- Written by
- WebJi® team
Last editorial update: 14 November 2026.
What is the .htaccess file, and where is it?
.htaccess is a plain-text file of Apache (or LiteSpeed) directives that applies to the folder it sits in and every folder below it; for a website it usually lives in the document root, such as public_html. Its name starts with a dot, so file managers hide it until hidden files are shown.
- Location:
/home/username/public_html/.htaccessfor the main domain; each addon domain has its own folder and file. - Permissions: 644. Over-permissive settings such as 777 cause 500 errors on many shared servers.
- Scope: the folder and its subfolders, unless a subfolder has its own file.
- Servers: Apache and LiteSpeed read it; Nginx ignores it completely.
WordPress writes a block between # BEGIN WordPress and # END WordPress and rewrites it when permalinks are saved, so put your own rules above or below those markers, never inside.
How do you open and back up .htaccess safely?
Copy the file to .htaccess.bak and download a second copy before editing, because one invalid line returns a 500 error on every page, including wp-admin. Then open it with whichever tool you already have.
| Route | How | Best when |
|---|---|---|
| cPanel File Manager | Settings → Show Hidden Files, then right-click .htaccess → Edit | Most edits; no extra software |
| Other panels | The panel's file manager with hidden files shown | ControlPanel.Systems, Plesk or DirectAdmin accounts |
| FTP / SFTP | FileZilla with "Force showing hidden files" on | The panel's editor will not save |
| SSH | cp .htaccess .htaccess.bak && nano .htaccess | Long files, quick test cycles |
Avoid word processors and editors that save a UTF-8 byte-order mark or smart quotes; either can make Apache reject the whole file. The cPanel File Manager guide covers the editor in detail.
Which rules are commonly added to .htaccess?
The most useful .htaccess rules force HTTPS, choose www or non-www, set browser caching, add security headers and block sensitive files. Wrap module-dependent rules in <IfModule> so a missing module does not cause a 500.
# 1. Force HTTPS (single server, no proxy in front)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 2. Redirect www to the bare domain
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
# 3. Browser caching for static files
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/webp "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule>
# 4. A security header
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
</IfModule>
# 5. Block a file (Apache 2.4 syntax)
<Files "xmlrpc.php">
Require all denied
</Files>
Behind Cloudflare or another proxy, %{HTTPS} can read "off" even for secure visits and cause a redirect loop; test with %{HTTP:X-Forwarded-Proto} instead, or set the redirect at the proxy. Apache 2.4 uses Require; the old Order/Allow/Deny lines work only through a compatibility module.
Redirect patterns are covered in depth in the .htaccess 301 redirect guide, and the .htaccess generator builds a starter file.
How should you edit .htaccess without breaking the site?
Add one block at a time, save, and test before adding the next; rules run top to bottom, so specific paths go above general ones. Twenty untested lines turn one mistake into twenty suspects.
- Comment changes: a line starting with
#is ignored; note the date and reason. - Case: directive names are case-insensitive, URL paths usually are not; add
[NC]to ignore case in a condition. - Right tool: a single-page redirect or PHP version change is often simpler in the hosting panel than in this file.
- Performance: Apache reads .htaccess on every request; on your own VPS, Apache's documentation recommends putting rules in the main server configuration instead.
How do you test a .htaccess change and roll it back?
Load the home page and one inner page in a private window, then check the response code of the URL the rule targets; if every page returns 500, rename .htaccess and restore the backup. Deleting or renaming the file brings the site back immediately with server defaults.
curl -I https://example.in/old-page/ # expect 301 and a Location header
curl -I http://example.in/ # expect 301 to https://
| Symptom | Likely cause |
|---|---|
| 500 on every page | Syntax error, unknown directive or byte-order mark |
| 403 on every page | A deny or Require rule that is too broad |
| "Too many redirects" | HTTPS or www rule looping, often behind a proxy or CDN |
| Rule does nothing | Wrong folder, Nginx server, or AllowOverride disabled |
| Rule disappears later | It was inside the WordPress block or a plugin rewrote the file |
Why is the .htaccess file missing or ignored?
Usually it is only hidden; turn on hidden files. If it truly does not exist, WordPress recreates it when you click Save Changes under Settings → Permalinks. A hand-made file must be named exactly .htaccess, saved as plain text, with permissions 644.
If the file exists but nothing in it takes effect, the server may be Nginx, or the host may have set AllowOverride None for that directory, which makes Apache ignore the file silently. On shared hosting that is a support question. If you are stuck after restoring the backup, email support@webji.services with the rule and the time you added it; the team replies 24/7 in Hindi and English. For Nginx servers, the equivalent rules belong in the server block, as shown in Nginx as a reverse proxy for Apache.
Frequently asked questions
Can I have more than one .htaccess file?
Yes. Each folder can have its own, and rules in a subfolder's file apply on top of the parent's for that subfolder. This is how a /blog/ WordPress install can have its own rewrite rules inside a site that uses different ones at the root.
Can I password-protect a folder with .htaccess?
Yes, with AuthType Basic, an AuthUserFile pointing to a .htpasswd file outside the web root, and Require valid-user. Most panels offer a "Directory Privacy" or similar tool that writes these lines for you, which avoids path mistakes.
Does .htaccess affect site speed?
Slightly, because the server checks for it on each request, and long rule lists add processing. On shared hosting the effect is small. Caching and compression rules in the file usually speed the site up far more than the file slows it down.
Can I change PHP settings in .htaccess?
Only when PHP runs as an Apache module, which is rare on modern shared hosting. With PHP-FPM or LiteSpeed, php_value lines can cause a 500 error. Use the panel's PHP settings or a .user.ini file instead.
Directives checked against Apache HTTP Server 2.4 documentation · Reviewed by the WebJi® team, Jaipur.
Ready to get started with WebJi®?
Join 12,000+ businesses across India and overseas. INR pricing, 18% GST shown before payment and 24/7 email support in Hindi and English — average first reply under 60 minutes.