Problem/Motivation

Rewrite rules for gzipped CSS and JavaScript aggregates in .htaccess never match, because regex lacks a quantifier for the character class.

Steps to reproduce

RewriteRule ^(.*css_[a-zA-Z0-9-_])\.css$ $1\.css\.gz [QSA]
RewriteRule ^(.*js_[a-zA-Z0-9-_])\.js$ $1\.js\.gz [QSA]

Proposed resolution

RewriteRule ^(.*css_[a-zA-Z0-9-_]+)\.css$ $1\.css\.gz [QSA]
RewriteRule ^(.*js_[a-zA-Z0-9-_]+)\.js$ $1\.js\.gz [QSA]

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

The htaccess rewrite rule that enables static gzipped JavaScript and CSS aggregates to be served to browsers has been modified to improve performance. Sites should update their .htaccess files to take advantage of this performance improvement.

Issue fork drupal-3336463

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

sleitner created an issue. See original summary.

sleitner’s picture

Issue summary: View changes
sleitner’s picture

Status: Active » Needs review
sleitner’s picture

Issue summary: View changes
cilefen’s picture

Status: Needs review » Active

There is no code to review at this time.

sleitner’s picture

Title: Rewrite rules for gzipped CSS and JavaScript aggregates do never match » Rewrite rules for gzipped CSS and JavaScript aggregates never match
Issue summary: View changes

sleitner’s picture

Status: Active » Needs review
sleitner’s picture

Version: 9.5.x-dev » 10.1.x-dev

sleitner’s picture

Title: Rewrite rules for gzipped CSS and JavaScript aggregates never match » Htaccess rewrite rules for gzipped CSS and JavaScript aggregates never match
recrit’s picture

@sleitner thanks for the patch. The patch fixed it for my build. It now correctly serves JS and CSS with "Content-Encoding: gzip".
My only thought is the quantifier might need to be a "+" instead of "*" since "js_.js" is not a valid filename.

The erroneous changes in #1040534: Rewrite rules for gzipped CSS and JavaScript aggregates cause lots of lstats for files that will never exist update the RewriteRule to the following:
RewriteRule ^(.*js_[a-zA-Z0-9-_])\.js$ $1\.js\.gz [QSA]

Example Drupal JS filename: "js_w0tbu2Mi0ldw2g1-6ceMRIw636XPaZoJEvQkqKjxQkE.js"
Bug: The current rule only matches filenames "js_w.js" - which does not match the Drupal JS filename.
Expected: This rule should match "js_w0tbu2Mi0ldw2g1-6ceMRIw636XPaZoJEvQkqKjxQkE.js"

sleitner’s picture

Issue summary: View changes

@recrit I changed "*" to "+", which should be better. Please review

recrit’s picture

@sleitner thanks, looks good and worked as expected correctly serving the gzip files

catch’s picture

Status: Needs review » Reviewed & tested by the community

I think I introduced the original bug here, fix looks good to me and preserves the behaviour the original issue was trying to fix (not looking for files that will never exist).

recrit’s picture

@catch Note that this is also a bug for 9.5.x. The 10.x patch applies cleanly to both branches but I also updated the 9.5.x MR with the latest commits.

MR Summary:
- 10.1.x: https://git.drupalcode.org/project/drupal/-/merge_requests/3431
- 9.5.x: https://git.drupalcode.org/project/drupal/-/merge_requests/3290

longwave’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 1d3e0326a5 to 10.1.x and 7f5e51d92b to 10.0.x and 03de0c83b9 to 9.5.x. Thanks!

Will open a followup to add test coverage to this, as we test other parts of .htaccess.

  • longwave committed 7f5e51d9 on 10.0.x
    Issue #3336463 by sleitner, recrit, catch: Htaccess rewrite rules for...

  • longwave committed 1d3e0326 on 10.1.x
    Issue #3336463 by sleitner, recrit, catch: Htaccess rewrite rules for...

  • longwave committed 03de0c83 on 9.5.x
    Issue #3336463 by sleitner, recrit, catch: Htaccess rewrite rules for...
alexpott’s picture

Issue summary: View changes
Issue tags: +9.5.8 release notes

I've added the same release note as #1040534: Rewrite rules for gzipped CSS and JavaScript aggregates cause lots of lstats for files that will never exist - I've also updated the CR. I'm note really sure what we should do for people who updated due to the original issue or started a site in the meantime. Maybe we need to update the original CR for this.

catch’s picture

I've made a new CR with the same wording - if people follow the list or anything, it ensures they'll see it again. Agreed with fixing the old one too though.

https://www.drupal.org/node/3351478

luenemann’s picture

Version: 10.1.x-dev » 9.5.x-dev

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.