Fix Pagination When Using Laravel Page Cache

Note: Below remarks apply to the Laravel Page Cache package.

While it is perfectly possible to disable caching for URLs that contain a query string, such routes may still get served a previously cached page. We need to not only prevent URLs containing a query string from being cached, but also prevent serving a cached page.

Luckily, doing so is easy and only requires slightly modified rewrite conditions in your site’s public/.htaccess file—note the added %{QUERY_STRING} conditions:

RewriteCond %{QUERY_STRING} ^$ [OR]
RewriteCond %{QUERY_STRING} ^page=1$ [NC]
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{DOCUMENT_ROOT}/page-cache/pc__index__pc.html -f
RewriteRule .? page-cache/pc__index__pc.html [L]

RewriteCond %{QUERY_STRING} ^$ [OR]
RewriteCond %{QUERY_STRING} ^page=1$ [NC]
RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.html -f
RewriteRule . page-cache%{REQUEST_URI}.html [L]

This way, a cached page’ll only get served if the query string is absent or equal to page=1.