ess plus 4 months"
ExpiresByType video/webm "access plus 4 months"
ExpiresByType image/avif "access plus 4 months"
ExpiresByType image/avif-sequence "access plus 4 months"
# HTC files (css3pie)
ExpiresByType text/x-component "access plus 1 month"
# Webfonts
ExpiresByType font/ttf "access plus 4 months"
ExpiresByType font/otf "access plus 4 months"
ExpiresByType font/woff "access plus 4 months"
ExpiresByType font/woff2 "access plus 4 months"
ExpiresByType image/svg+xml "access plus 4 months"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
# CSS and JavaScript
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
HTACCESS;
/**
* Filter rules to improve performances with Expires Headers
*
* @since 1.0
*
* @param string $rules Rules that will be printed.
*/
$rules = apply_filters( 'rocket_htaccess_mod_expires', $rules );
return $rules;
}
/**
* Rules for default charset on static files
*
* @since 1.0
*
* @return string $rules Rules that will be printed
*/
function get_rocket_htaccess_charset() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
// Get charset of the blog.
$charset = preg_replace( '/[^a-zA-Z0-9_\-\.:]+/', '', get_bloginfo( 'charset', 'display' ) );
if ( empty( $charset ) ) {
return '';
}
$rules = "# Use $charset encoding for anything served text/plain or text/html" . PHP_EOL;
$rules .= "AddDefaultCharset $charset" . PHP_EOL;
$rules .= "# Force $charset for a number of file formats" . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= "AddCharset $charset .atom .css .js .json .rss .vtt .xml" . PHP_EOL;
$rules .= '' . PHP_EOL . PHP_EOL;
/**
* Filter rules for default charset on static files
*
* @since 1.0
*
* @param string $rules Rules that will be printed.
*/
$rules = apply_filters( 'rocket_htaccess_charset', $rules );
return $rules;
}
/**
* Rules for cache control
*
* @since 1.1.6
*
* @return string $rules Rules that will be printed
*/
function get_rocket_htaccess_files_match() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
$rules = '' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= 'Header set X-Powered-By "WP Rocket/' . WP_ROCKET_VERSION . '"' . PHP_EOL;
$rules .= 'Header unset Pragma' . PHP_EOL;
$rules .= 'Header append Cache-Control "public"' . PHP_EOL;
$rules .= 'Header unset Last-Modified' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '' . PHP_EOL . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= 'Header unset Pragma' . PHP_EOL;
$rules .= 'Header append Cache-Control "public"' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '' . PHP_EOL . PHP_EOL;
/**
* Filter rules for cache control
*
* @since 1.1.6
*
* @param string $rules Rules that will be printed.
*/
$rules = apply_filters( 'rocket_htaccess_files_match', $rules );
return $rules;
}
/**
* Rules to remove the etag
*
* @since 1.0
*
* @return string $rules Rules that will be printed
*/
function get_rocket_htaccess_etag() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
$rules = '# FileETag None is not enough for every server.' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= 'Header unset ETag' . PHP_EOL;
$rules .= '' . PHP_EOL . PHP_EOL;
$rules .= '# Since we’re sending far-future expires, we don’t need ETags for static content.' . PHP_EOL;
$rules .= '# developer.yahoo.com/performance/rules.html#etags' . PHP_EOL;
$rules .= 'FileETag None' . PHP_EOL . PHP_EOL;
/**
* Filter rules to remove the etag
*
* @since 1.0
*
* @param string $rules Rules that will be printed.
*/
$rules = apply_filters( 'rocket_htaccess_etag', $rules );
return $rules;
}
/**
* Rules to Cross-origin fonts sharing when CDN is used
*
* @since 2.4
*
* @return string $rules Rules that will be printed
*/
function get_rocket_htaccess_web_fonts_access() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
if ( ! get_rocket_option( 'cdn', false ) ) {
return;
}
$rules = '# Send CORS headers if browsers request them; enabled by default for images.' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '# mod_headers, y u no match by Content-Type?!' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= 'SetEnvIf Origin ":" IS_CORS' . PHP_EOL;
$rules .= 'Header set Access-Control-Allow-Origin "*" env=IS_CORS' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '' . PHP_EOL . PHP_EOL;
$rules .= '# Allow access to web fonts from all domains.' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= 'Header set Access-Control-Allow-Origin "*"' . PHP_EOL;
$rules .= '' . PHP_EOL;
$rules .= '' . PHP_EOL . PHP_EOL;
// @codingStandardsIgnoreEnd
/**
* Filter rules to Cross-origin fonts sharing
*
* @since 1.0
*
* @param string $rules Rules that will be printed.
*/
$rules = apply_filters( 'rocket_htaccess_web_fonts_access', $rules );
return $rules;
}
/**
* Tell if WP rewrite rules are present in a given string.
*
* @since 3.2.4
* @author Grégory Viguier
*
* @param string $content Htaccess content.
* @return bool
*/
function rocket_has_wp_htaccess_rules( $content ) {
if ( is_multisite() ) {
$has_wp_rules = strpos( $content, '# add a trailing slash to /wp-admin' ) !== false;
} else {
$has_wp_rules = strpos( $content, '# BEGIN WordPress' ) !== false;
}
/**
* Tell if WP rewrite rules are present in a given string.
*
* @since 3.2.4
* @author Grégory Viguier
*
* @param bool $has_wp_rules True when present. False otherwise.
* @param string $content .htaccess content.
*/
return apply_filters( 'rocket_has_wp_htaccess_rules', $has_wp_rules, $content );
}
/**
* Check if WP Rocket htaccess rules are already present in the file
*
* @since 3.3.5
* @author Remy Perona
*
* @return bool
*/
function rocket_check_htaccess_rules() {
if ( ! function_exists( 'get_home_path' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$htaccess_file = get_home_path() . '.htaccess';
if ( ! rocket_direct_filesystem()->is_readable( $htaccess_file ) ) {
return false;
}
$htaccess = rocket_direct_filesystem()->get_contents( $htaccess_file );
if ( preg_match( '/\s*# BEGIN WP Rocket.*# END WP Rocket\s*?/isU', $htaccess ) ) {
return true;
}
return false;
}