| 1 |
<?php exit; |
| 2 |
/** |
| 3 |
* Autoptimize's magic 404 handler. |
| 4 |
* |
| 5 |
* Configure your webserver to have requests for files that are no longer in |
| 6 |
* /wp-content/cache/autoptimize/ to redirect to this file. AO's .htaccess file |
| 7 |
* will have a "Errordocument:" directive to automatically do this. |
| 8 |
* |
| 9 |
* This file has simple logic to redirect to the "fallback" files that are |
| 10 |
* created automatically by AO to avoid visitors seeing broken pages or |
| 11 |
* Googlebot getting utterly confused. |
| 12 |
* |
| 13 |
* Error logging is off by default (don't want to flood your php errorlog, but |
| 14 |
* can be enabled by this code snippet: |
| 15 |
* |
| 16 |
* add_filter( 'autoptimize_filter_cache_fallback_log_errors', '__return_true' ); |
| 17 |
* |
| 18 |
* Warning: the fallback files might not apply to all pages, so this is a just |
| 19 |
* a temporary solution, you really should clear any page cache to avoid requests |
| 20 |
* to files that don't exist in AO's cache. |
| 21 |
*/ |
| 22 |
|
| 23 |
$original_request = strtok( $_SERVER['REQUEST_URI'], '?' ); |
| 24 |
|
| 25 |
if ( strpos( $original_request, 'uucss/uucss-' ) !== false ) { |
| 26 |
$original_request = preg_replace( '/uucss\/uucss-[a-z0-9]{32}-/', 'css/', $original_request ); |
| 27 |
} |
| 28 |
|
| 29 |
$fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request ); |
| 30 |
$ao_cache_dir = '<!--ao-cache-dir-->'; |
| 31 |
$js_or_css = pathinfo( $original_request, PATHINFO_EXTENSION ); |
| 32 |
|
| 33 |
// add multisite logic. |
| 34 |
$multisite = false; |
| 35 |
if ( true === $multisite ) { |
| 36 |
preg_match( '#\/([0-9]{1,5})\/(?:js|css)\/[a-z0-9]*_fallback\.(?:js|css)$#', $fallback_target, $child_site_id ); |
| 37 |
$ao_root_cache_dir = preg_replace( '#[0-9]*\/$#', '', $ao_cache_dir ); |
| 38 |
$ao_cache_dir = $ao_root_cache_dir . $child_site_id[1] . '/'; |
| 39 |
} |
| 40 |
|
| 41 |
$fallback_path = $ao_cache_dir . $js_or_css . '/<!--ao-cachefile-prefix-->fallback.' . $js_or_css; |
| 42 |
|
| 43 |
if ( $original_request !== $fallback_target && file_exists( $fallback_path ) ) { |
| 44 |
// error_log( 'Autoptimize file ' . $original_request . ' not found, using fallback instead.' ); |
| 45 |
header( 'HTTP/1.1 301 Moved Permanently' ); |
| 46 |
header( 'Location: ' . $fallback_target ); |
| 47 |
} else { |
| 48 |
// error_log( 'Autoptimize file ' . $original_request . ' not found, sending 410 gone response.' ); |
| 49 |
header( 'HTTP/1.1 410 Gone' ); |
| 50 |
} |
| 51 |
|
| 52 |
exit(); |
| 53 |
|