WpW: .htaccess and Rewrite Rules

July 26th, 2017 by LSCache 4 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.

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 public 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.

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, too, uses .htaccess. Here’s an example of the LSCache-related lines you might find in your .htaccess:

<IfModule LiteSpeed>
CacheLookup public on
RewriteEngine on
RewriteRule wp-content/.*/(loader|fonts)\.php - [E=cache-control:max-age=3600]
RewriteRule favicon\.ico$ - [E=cache-control:max-age=86400]

The exact lines you see for WordPress and LSCache may not match these examples directly, but they should be pretty similar.

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

  1. Public Cache Lookup must be enabled via the following directive: <IfModule LiteSpeed> CacheLookup public on </IfModule>
  2. The block which defines the rewrite rules for loader, fonts, and favicon must appear before any WordPress rewrite rules.

If you’re noticing quirky behavior, take a look at your configuration file. 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:


Related Posts