WpW: .htaccess and Rewrite Rules

July 26th, 2017 by LSCache 6 Comments

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]
# 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:

## 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! ##

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.


Related Posts