WpW: .htaccess and Rewrite Rules

Written by

in

Welcome to another installment of WordPress Wednesday!

We’ve covered a lot of complex topics here lately, so how about we give you a nice quick tip for a change? Today, we’ve got a little tidbit related to your .htaccess file and LiteSpeed Cache Rewrite Rules.

What is .htaccess?

LiteSpeed Web Server supports the Apache .htaccess file. This configuration file is important because it allows us to enable or disable additional functionality, and to define URL redirect rules, among other things.

When you look at the .htaccess in your WordPress root directory, there are two things you should notice:

  1. There should be a line that reads CacheLookup on
  2. WordPress’ URL rewrite rules (if there are any) should come after LiteSpeed’s Cache rules.

Under normal circumstances, this is not something you should have to worry about. There should not be any need for you to edit this file yourself. LiteSpeed’s WP plugin maintains the .htaccess file automatically. When the plugin is installed or uninstalled, enabled or disabled, or when you change relevant settings in your WP dashboard, .htaccess is updated to reflect that change.

In some more detail

If you have chosen to use pretty permalinks (i.e. your WordPress URLs look more like example.com/post-title or example.com/2017/07/post-title than example.com/?p=123), then WP has used the .htaccess file in its root directory to define some rewrite rules. Depending on how many plugins you’ve got installed and a variety of other factors, your .htaccess could be fairly simple like the one below, or it could be really hairy with many different blocks belonging to a variety of plugins and other software.

WordPress Rewrite Rules

This is an example of the lines that a basic WP installation would insert into .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

LiteSpeed Cache Rewrite Rules

LiteSpeed Cache, too, uses .htaccess. Here’s an example of the LSCache-related lines you might find in your .htaccess:

# BEGIN LSCACHE
## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##

RewriteEngine on
CacheLookup on
RewriteRule .* - [E=Cache-Control:no-autoflush]
RewriteRule \.litespeed_conf\.dat - [F,L]

### marker CACHE RESOURCE start ###
RewriteRule wp-content/.*/[^/]*(responsive|css|js|dynamic|loader|fonts)\.php - [E=cache-control:max-age=3600]
### marker CACHE RESOURCE end ###

### marker FAVICON start ###
RewriteRule favicon\.ico$ - [E=cache-control:max-age=86400]
### marker FAVICON end ###

### marker WEBP start ###
RewriteCond %{HTTP_ACCEPT} "image/webp" [or]
RewriteCond %{HTTP_USER_AGENT} "Page Speed"
RewriteRule .* - [E=Cache-Control:vary=%{ENV:LSCACHE_VARY_VALUE}+webp]
RewriteCond %{HTTP_USER_AGENT} iPhone.*Version/(\d{2}).*Safari
RewriteCond %1 >13
RewriteRule .* - [E=Cache-Control:vary=%{ENV:LSCACHE_VARY_VALUE}+webp]
### marker WEBP end ###

### marker DROPQS start ###
CacheKeyModify -qs:fbclid
CacheKeyModify -qs:gclid
CacheKeyModify -qs:utm*
CacheKeyModify -qs:_ga
### marker DROPQS end ###


## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
# END LSCACHE

The exact lines you see for WordPress and LSCache depend on which options you have enabled in the plugin, and may not match these examples directly, but they should be pretty similar.

Putting Them Together

In order for LSCache to work properly, two things must be true:

  1. Public Cache Lookup must be enabled via the CacheLookup on directive.
  2. The LITESPEED WP CACHE PLUGIN block must appear before any WordPress rewrite rules.

If you’re noticing quirky behavior, take a look at your configuration file. You can view it via WP Dashboard > LiteSpeed Cache > Toolbox > View .htaccess. Like we mentioned earlier, LSCache for WordPress automatically handles cache-related changes to .htaccess, but if it has been manually updated or otherwise manipulated by another plugin, you may find irregularities that need to be corrected.

Have some of your own ideas for future WordPress Wednesday topics? Leave us a comment!

Don’t forget to meet us back here next week for the next installment. In the meantime, here are a few other things you can do:


This content was last verified and updated in October of 2022. If you find an inaccuracy, please let us know! In the meantime, see our documentation site for the most up-to-date information.

Comments

10 responses to “WpW: .htaccess and Rewrite Rules”

  1. Niall Chen Avatar
    Niall Chen

    Hi..there
    How to allow from a url not a ip address ?
    ex.

    order deny,allow
    deny from all

    allow from http://www.abc.com

  2. Jackson Avatar
    Jackson

    LiteSpeed does not support “host” at the moment hence “Allow from abc.com” may not work. He may have to use IP at this stage.

  3. Maswood Avatar

    Please help me my website uses the Litespeed server I want to add .htpasswd to prevent brute-force attacks please help me Litespeed.

    1. Lisa Clarke Avatar
      Lisa Clarke

      I’m afraid I don’t know anything about .htpasswd. Have you looked at LiteSpeed Web Server’s built-in protection against WP Brute-Force attacks? Here is more information about that.

      If you would still like help with .htpasswd, you can contact our support team at support@litespeedtech.com and they may be able to give you some guidance on that.

  4. Lorenzo P. Avatar
    Lorenzo P.

    Hello, i set up the LSC plug in for my website, then i deleted because of some troubles with CDN. But still remaing some lines in the .htaccess file . These consecutive lines:
    # BEGIN LSCACHE
    # END LSCACHE
    # BEGIN NON_LSCACHE
    # END NON_LSCACHE
    Can i cancel them? Or not?
    Thank you very much, waiting an answer.

    1. Lisa Clarke Avatar
      Lisa Clarke

      Hi, Lorenzo. Yes, you can safely remove those lines if you are no longer using the LiteSpeed Cache plugin.

  5. said Avatar

    is the htaccess file gaving hackers access to the website that using litespeed?

    1. Lisa Clarke Avatar
      Lisa Clarke

      No. LiteSpeed doesn’t add anything to your .htaccess file that would cause you to be hacked.

  6. Mading T Avatar

    I recently added LiteSpeed plugin and working wonderfully but some issues on Safari/Firefox/Mobile where some collections such as https://nascostonetile.com/type/decorative-wall-collections/ Do not load products properly on these browsers.

    Chrome is loading really for the most part except on mobile for the same collection links.

    How can I resolve this issue for these browser including mobile? Any tips

    1. Lisa Clarke Avatar
      Lisa Clarke

      The only thing I can think of off the top of my head is to make sure that you have Cache Mobile enabled. If you do have that enabled, and you still are having these issues, your best bet is to contact support for help at support@litespeedtech.com. Hope this helps!

Leave a Reply

Your email address will not be published. Required fields are marked *