Common .htaccess Rules: Redirects, Blocking, Caching and Security Headers

The .htaccess file is a small text file that tells the web server how to handle requests for your site -- where to redirect them, who to turn away, how long browsers should cache files and which security headers to send. This guide collects the common .htaccess rules our customers ask for most, explains where to put them so they don't get overwritten, and shows how to recover if a rule takes your site down.

Does .htaccess work on LiteSpeed?

Yes. Instant Access Internet Services servers run the LiteSpeed web server, which reads Apache-style .htaccess files and understands the same common directives: RewriteRule, Redirect, Header, ExpiresByType, Require, ErrorDocument and so on. Changes take effect as soon as you save the file -- there's nothing to restart.

Rules apply to the folder the file is in and every folder below it. The main file for your website lives in public_html. A folder further down (for example public_html/wp-content/uploads) can have its own .htaccess that adds rules just for that folder.

Before you edit: always make a backup

One typo in .htaccess can turn your whole site into a 500 Internal Server Error. The fix is easy if you kept a copy, so do this every time:

  1. In cPanel, go to Files > File Manager and open public_html. If you can't see .htaccess, click Settings, tick Show Hidden Files (dotfiles) and click Save.
  2. Right-click .htaccess, choose Copy, and copy it to /public_html/.htaccess-backup-2026-09-28 (use today's date).
  3. Right-click the original .htaccess and choose Edit. Make one change, click Save Changes, and load your site in a private browser window.
  4. If the site breaks, open the file again and remove what you added, or copy your backup back over it.

If you forgot the backup, JetBackup 5 keeps recent copies of the file. See How to Restore a File or Folder with JetBackup 5.

Leave the marker blocks alone

Several tools write their own sections into .htaccess and mark them with comment lines. They rewrite everything between their markers whenever you change a setting, so anything you type inside a block will eventually vanish. The ones you'll see most often:

Marker linesWritten byWhat it does
# BEGIN WordPress ... # END WordPressWordPress, when you save Settings > PermalinksSends every request that isn't a real file to WordPress so pretty permalinks work.
# BEGIN LSCACHE ... # END LSCACHEThe LiteSpeed Cache pluginPage cache rules. The block says "Do not edit the contents of this block!" -- take it at its word.
# BEGIN NON_LSCACHE ... # END NON_LSCACHEThe LiteSpeed Cache pluginExtra rules such as browser caching, if you turned that on in the plugin.
# php -- BEGIN cPanel-generated handler, do not editcPanel's MultiPHP ManagerTells the server which PHP version runs your .php files.
# BEGIN cPanel-generated php ini directives, do not editcPanel's MultiPHP INI EditorPHP settings such as memory_limit.

The simple rule: put your own rules at the very top of the file, above all of these blocks, and label them with your own comment so you recognize them later, for example # My rules - added 2026-09-28. The top matters for redirects too -- if a request reaches the WordPress block first, WordPress handles it and your redirect further down never runs.

Redirect rules

We already have step-by-step guides for the redirects people need most, so use those rather than copying rules from random websites:

One pattern those guides don't cover is renaming a whole folder while keeping every page inside it. RedirectMatch does it in one line:

RedirectMatch 301 ^/blog/(.*)$ https://example.com/news/$1

Now /blog/spring-sale/ goes to /news/spring-sale/. The (.*) captures the rest of the address and $1 puts it back.

Blocking rules

Block one IP address or a range

cPanel's IP Blocker tool isn't included on standard plans, but you can do the same thing in .htaccess:

<RequireAll>
    Require all granted
    Require not ip 203.0.113.45
    Require not ip 198.51.100.0/24
</RequireAll>

The first line lets everyone in; the next lines make exceptions. The blocked visitor sees 403 Forbidden. The /24 form blocks a whole range (198.51.100.0 to 198.51.100.255). Blocking by IP is a blunt tool -- attackers change addresses constantly, and the server's own security (Imunify360) already filters a lot of bad traffic -- so use it for one persistent pest, not as your main defense.

Block a bad bot by its user agent

If your access log (Metrics > Visitors or Metrics > Raw Access) shows one scraper hammering your site, block it by the name in its user agent:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (BadBotName|AnotherScraper) [NC]
RewriteRule .* - [F,L]

[NC] makes the match case-insensitive and [F] returns 403 Forbidden. Be careful not to match something common like bot on its own, or you'll block Google and Bing too.

Keep private files private

Some files should never be downloadable by a browser. This blocks direct access to WordPress's configuration file, environment files and backup copies of .htaccess:

<FilesMatch "^(wp-config\.php|\.env|\.htaccess-backup.*)$">
    Require all denied
</FilesMatch>

WordPress still reads wp-config.php normally -- this only stops people requesting it over the web.

Stop PHP running in your uploads folder

Hackers who find a weak upload form try to drop a PHP file into wp-content/uploads and run it. Create a separate .htaccess inside public_html/wp-content/uploads containing:

<FilesMatch "\.(php|phtml|php[0-9])$">
    Require all denied
</FilesMatch>

WP Toolkit's security hardening can apply similar protections for you -- see How to Secure WordPress with WP Toolkit Security Hardening.

Turn off folder listings

Options -Indexes

This stops visitors seeing a list of files in any folder that has no index page. You can also do it without editing: How to Turn Directory Listings On or Off in cPanel (Indexes).

Browser caching rules

Browser caching tells visitors' browsers to keep images, stylesheets and scripts for a while instead of downloading them on every page. It makes repeat visits noticeably faster.

WordPress with LiteSpeed Cache: don't add these rules by hand. Turn on Browser Cache in LiteSpeed Cache > Cache > Browser and the plugin writes the rules into its NON_LSCACHE block. Two sets of rules for the same thing just cause confusion.

Other sites (plain HTML, other software) can use:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType font/woff2 "access plus 1 year"
</IfModule>

Keep HTML pages out of this list. If you cache the pages themselves in the browser, visitors may keep seeing an old version after you update the site. If you change a stylesheet and a long cache stops visitors seeing it, rename the file or add a version number to its link (for example style.css?v=2).

Security header rules

Security headers are instructions the browser follows to protect your visitors. These four are safe for almost every site:

<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
</IfModule>
  • X-Content-Type-Options stops browsers guessing a file's type, which blocks some tricks with uploaded files.
  • X-Frame-Options stops other sites showing your pages inside a frame (clickjacking). If you deliberately embed your site elsewhere, leave this one out.
  • Referrer-Policy limits how much of your page addresses is passed to other sites when visitors click a link.
  • Permissions-Policy turns off camera, microphone and location access. Remove an item if your site genuinely uses it (a store locator using location, for example).

A warning about HSTS

Strict-Transport-Security (HSTS) tells browsers to refuse plain http for your domain for a set time. It's a good header, but it's hard to undo: browsers remember it for the whole period you set. Only add it once https works perfectly on every part of your domain, and start with a short time:

Header always set Strict-Transport-Security "max-age=300"

After a week without problems you can raise it (max-age=31536000 is one year). Avoid the includeSubDomains and preload options unless you're sure every subdomain has a working certificate.

Check your headers are being sent

Open a private window, press F12, open the Network tab, reload, click the first request and look under Response Headers. From Advanced > Terminal in cPanel you can also run:

curl -sI https://example.com/ | grep -i -E "x-frame|x-content|referrer|permissions|strict"

Troubleshooting

My site shows a 500 error right after I saved

The file has a syntax error: a missing </IfModule> or </FilesMatch>, a stray character, or a directive the server doesn't allow. Undo your change or copy your backup back. In cPanel, Metrics > Errors usually names the bad line -- look for messages mentioning .htaccess or "Invalid command".

My rule does nothing

  • Check the rule is above the # BEGIN WordPress block, not below or inside it.
  • Browsers remember 301 redirects and cached files. Test in a private window.
  • Check you edited the right file. A subdomain has its own folder and its own .htaccess.
  • For WordPress, purge LiteSpeed Cache -- an already-cached page can hide a change.

My rules disappeared

You probably put them inside a marker block, and a plugin or cPanel rewrote it. Add them again at the top of the file, outside every block.

I blocked myself

A website block in .htaccess doesn't affect cPanel. Log in to cPanel, open File Manager and remove the rule. If you can't reach cPanel, webmail or email either, that's a different problem -- your IP address may have been blocked by the server firewall after failed logins. Open a ticket with your IP address from whatismyipaddress.com.

"Too many redirects" after adding a rule

Your new redirect is fighting with another one. See ERR_TOO_MANY_REDIRECTS: How to Fix a Redirect Loop on Your Website.

Common questions

Can I set PHP settings like memory_limit in .htaccess?

Use Software > MultiPHP INI Editor instead. It writes the settings in the right places for you. Old php_value lines copied from a previous host can conflict with it or cause errors -- see How to Change PHP Settings (memory_limit, upload_max_filesize).

Do I need "RewriteEngine On" before every rewrite rule?

Once per file is enough, before the first RewriteCond or RewriteRule. Having it more than once doesn't hurt.

I found rules online with "Order Allow,Deny". Should I use them?

That's the old Apache 2.2 style. Use the Require style shown in this guide instead, and don't mix the two styles in the same block.

Does a large .htaccess slow my site down?

A few dozen lines make no noticeable difference. Hundreds of individual redirects might; if you have that many, a redirect plugin in WordPress is easier to manage.

Related guides

Still stuck? Open a support ticket and the Instant Access Internet Services team will help.

Ultrafast LiteSpeed hosting from InstantAccess.net: free SSL, free backups, cPanel included, no contracts. Check out our $10/month hosting.
  • htaccess rules, htaccess block ip, htaccess security headers, htaccess browser caching, block bot htaccess, litespeed htaccess, htaccess examples cpanel
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to Change Your PHP Version in cPanel

Running a current PHP version keeps your website faster and more secure, and many WordPress...

How to Change PHP Settings (memory_limit, upload_max_filesize)

If WordPress says "The uploaded file exceeds the upload_max_filesize directive", a plugin runs...

How to Set Up a Cron Job in cPanel

A cron job runs a command or script on a schedule, such as every hour or once a night, without...

How to Check Your Website Error Log and Recent Visitors

When your website shows a blank page, a "500 Internal Server Error" or a feature just stops...

How to View Website Statistics with AWStats and Bandwidth

Your Instant Access Internet Services hosting account includes built-in website statistics, so...