WP Rocket & Redirects

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

By default, Wordpress seems to redirect URLs without trailing slash to URLs with trailing slash. But WP Rocket seems to thwart this.

  • This mostly plays a role when not logged in, as WP Rocket is usually only active when users are not logged in
  • Context for this article: We use Apache and always SSL

When this happens

Since version 3.13 (2023.08), WP Rocket respects the WordPress settings, provided [1]:

  1. No WP Rocket rewrite rules in the root .htaccess file
  2. There are separate cache files for mobile devices
  3. No server cache.

Options

  1. Meet the WP Rocket requirements (previous section) for this to work » Didn't seem very realistic
  2. Use the WP Rocket Helper Plugin [2]
  3. Use .htaccess solutions (mentioned in sources)

Use WP Rocket Helper Plugin

See article WP Rocket Trailing Slash Helper Plugin

Use .htaccess solutions

Effectively, all the WP Rocket Trailing Slash Helper Plugin does, is add a section to .htaccess to do exactly this, so I rather use their code (but probably without the overhead of having it in a plugin).

See WP Rocket Trailing Slash Helper Plugin for the actual code.

Solution

Use the .htaccess code from WP Rocket Trailing Slash Helper Plugin and include it manually at the top in the .htaccess file of affected sites.

A heavily commented version of this code:

################################################################################
# ETS - Enforce Trailing Slash
################################################################################
#
# * This is the code from the "WP Rocket Trailing Slash Helper Plugin", but
#   installed without the actual plugin
# * https://wiki.devliegendebrigade.nl/WP_Rocket_Trailing_Slash_Helper_Plugin
# * https://wiki.devliegendebrigade.nl/WP_Rocket_%26_Redirects
# * https://docs.wp-rocket.me/article/131-redirection-to-enforce-trailing-slash-on-urls
#
#
# Location & conflict
########################################
#
# * I assumed, that ideally, both this rewrite + the WP Rocket Cache Rewrite
#   should be done. However, after realizing that this WP Rocket Helper Plugin
#   doesn't try that either, this may not be a problem after all.
# * What I thought was the actual problem: All rewrite sections in this file contain
#   a [L] flag. I can't remove any of these, so only one rewrite section at a
#   time gets executed. Again: After all, this might not be a problem at all
#
#
# Start here!
########################################
#
RewriteEngine On
#
#
# Conditions
########################################
#
# * It should not be a filename
# * Only relevant for GET requests
# * Not for requests that already end with a slash
# * Not for wp-json files
# * Not for various file names
# * Surprisingly, behaviour is as desired concerning local anchors, root URL
#   and parameter queries - No idea how that happened
#
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !^/wp-json/
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpg|png|jpeg|css|xml|txt|js|php|scss|webp|mp3|avi|wav|mp4|mov|pdf|html|htm|xst)$ [NC]
#
#
# Rewrite
########################################
#
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [R=301,L]

See also

Sources