Skip to content

Commit b4cc2d1

Browse files
committed
Fix Nginx rules again
1 parent 7a8bb4f commit b4cc2d1

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

BrowserCache_Environment_Nginx.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -300,22 +300,27 @@ private function generate_section( &$rules, $mime_types, $section ) {
300300
$subrules = Dispatcher::nginx_rules_for_browsercache_section( $this->c, $section );
301301
$rules .= ' ' . implode( "\n ", $subrules ) . "\n";
302302

303-
if ( ! $this->c->get_boolean( 'browsercache.no404wp' ) ) {
304-
$wp_uri = network_home_url( '', 'relative' );
305-
$wp_uri = rtrim( $wp_uri, '/' );
306-
$extensions_active = $this->c->get_array( 'extensions.active' );
307-
308-
if ( 'other' === $section && array_key_exists( 'imageservice', $extensions_active ) ) {
309-
$rules .= "\n" . ' location ~* ^(?<path>.+)\.(jpe?g|png|gif)$ {' . "\n" .
310-
' if ( $http_accept !~* "webp|\*/\*" ) {' . "\n" .
311-
' break;' . "\n" .
312-
' }' . "\n\n" .
313-
' ' . implode( "\n ", Dispatcher::nginx_rules_for_browsercache_section( $this->c, $section, true ) ) . "\n" .
314-
' add_header Vary Accept;' . "\n" .
315-
' try_files ${path}.webp $uri /index.php?$args;' . "\n" .
316-
' }' . "\n\n";
303+
// Add rules for the Image Service extension, if active.
304+
if ( 'other' === $section && array_key_exists( 'imageservice', $this->c->get_array( 'extensions.active' ) ) ) {
305+
$rules .= "\n" . ' location ~* ^(?<path>.+)\.(jpe?g|png|gif)$ {' . "\n" .
306+
' if ( $http_accept !~* "webp|\*/\*" ) {' . "\n" .
307+
' break;' . "\n" .
308+
' }' . "\n\n" .
309+
' ' . implode( "\n ", Dispatcher::nginx_rules_for_browsercache_section( $this->c, $section, true ) ) . "\n" .
310+
' add_header Vary Accept;' . "\n";
311+
312+
if ( $this->c->get_boolean( 'browsercache.no404wp' ) ) {
313+
$rules .= ' try_files ${path}.webp $uri =404;';
314+
} else {
315+
$rules .= ' try_files ${path}.webp $uri /index.php?$args;';
317316
}
318317

318+
$rules .= "\n" . ' }' . "\n\n";
319+
}
320+
321+
if ( ! $this->c->get_boolean( 'browsercache.no404wp' ) ) {
322+
$wp_uri = network_home_url( '', 'relative' );
323+
$wp_uri = rtrim( $wp_uri, '/' );
319324
$rules .= ' try_files $uri $uri/ ' . $wp_uri . '/index.php?$args;' . "\n";
320325
}
321326

Extension_ImageService_Environment.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,17 @@ private function rules_generate() {
147147
$config = Dispatcher::config();
148148

149149
/*
150-
* Add Nginx rules only if Browser Cache is disabled or no404wp is enabled.
150+
* Add Nginx rules only if Browser Cache is disabled.
151151
* Otherwise, the rules are added in "BrowserCache_Environment_Nginx.php".
152152
* @see BrowserCache_Environment_Nginx::generate_section()
153153
*/
154-
if ( $config->get_boolean( 'browsercache.no404wp' ) || ! $config->get_boolean( 'browsercache.enabled' ) ) {
154+
if ( ! $config->get_boolean( 'browsercache.enabled' ) ) {
155+
if ( $config->get_boolean( 'browsercache.no404wp' ) ) {
156+
$fallback = '=404';
157+
} else {
158+
$fallback = '/index.php?$args';
159+
}
160+
155161
return '
156162
# BEGIN W3TC WEBP
157163
location ~* ^(?<path>.+)\.(jpe?g|png|gif)$ {
@@ -162,7 +168,7 @@ private function rules_generate() {
162168
' . implode( "\n ", Dispatcher::nginx_rules_for_browsercache_section( $config, 'other' ) ) . '
163169
164170
add_header Vary Accept;
165-
try_files ${path}.webp $uri =404;
171+
try_files ${path}.webp $uri ' . $fallback . ';
166172
}
167173
# END W3TC WEBP
168174

0 commit comments

Comments
 (0)