| 1 |
<?php |
| 2 |
/** |
| 3 |
* The template used to generate our htaccess configuration. |
| 4 |
* |
| 5 |
* Start and End tags are added with the modifier. |
| 6 |
* |
| 7 |
* @see \SolidWP\Performance\Cache_Delivery\Htaccess\Modifier |
| 8 |
* |
| 9 |
* @var string $base The RewriteBase to use. |
| 10 |
* @var string $cache_path The relative path from WP_CONTENT_DIR without slashes, e.g. wp-content/cache/solid-performance/page |
| 11 |
* @var string $site_cache_path The absolute path to the cache files, .e.g /app/wp-content/cache/solid-performance/page/$host |
| 12 |
* @var string $extensions_regex The FilesMatch extensions regex for our cache files, e.g. html|gz. |
| 13 |
* |
| 14 |
* @package SolidWP\Performance |
| 15 |
*/ |
| 16 |
|
| 17 |
declare( strict_types=1 ); |
| 18 |
|
| 19 |
$htaccess = <<<RULES |
| 20 |
<IfModule mod_expires.c> |
| 21 |
ExpiresActive on |
| 22 |
ExpiresByType text/html "access plus 0 seconds" |
| 23 |
</IfModule> |
| 24 |
|
| 25 |
<IfModule mod_headers.c> |
| 26 |
Header set Referrer-Policy "no-referrer-when-downgrade" |
| 27 |
</IfModule> |
| 28 |
|
| 29 |
# Avoid recompressing already compressed files |
| 30 |
<IfModule mod_setenvif.c> |
| 31 |
SetEnvIfNoCase Request_URI \.gz$ no-gzip |
| 32 |
</IfModule> |
| 33 |
|
| 34 |
<IfModule mod_rewrite.c> |
| 35 |
RewriteEngine On |
| 36 |
RewriteBase $base |
| 37 |
|
| 38 |
# ---------------------------------- |
| 39 |
# Default env values |
| 40 |
# ---------------------------------- |
| 41 |
RewriteRule .* - [E=SWPSP_SCHEME:-http] |
| 42 |
RewriteRule .* - [E=SWPSP_EXT:html] |
| 43 |
RewriteRule .* - [E=SWPSP_DEVICE:] |
| 44 |
RewriteRule .* - [E=SWPSP_DEVICE_LABEL:desktop] |
| 45 |
|
| 46 |
# ---------------------------------- |
| 47 |
# Scheme detection |
| 48 |
# ---------------------------------- |
| 49 |
RewriteCond %{HTTPS} on [OR] |
| 50 |
RewriteCond %{SERVER_PORT} ^443$ [OR] |
| 51 |
RewriteCond %{HTTP:X-Forwarded-Proto} https |
| 52 |
RewriteRule .* - [E=SWPSP_SCHEME:-https] |
| 53 |
|
| 54 |
# ---------------------------------- |
| 55 |
# Mobile detection |
| 56 |
# ---------------------------------- |
| 57 |
RewriteCond %{HTTP_USER_AGENT} "(android.*mobile|iphone|ipod|windows phone|blackberry|bb10|opera mini|mobile.*safari)" [NC] |
| 58 |
RewriteRule .* - [E=SWPSP_DEVICE:-mobile,E=SWPSP_DEVICE_LABEL:mobile] |
| 59 |
|
| 60 |
# ---------------------------------- |
| 61 |
# Gzip detection |
| 62 |
# ---------------------------------- |
| 63 |
<IfModule mod_deflate.c> |
| 64 |
<IfModule mod_mime.c> |
| 65 |
AddType text/html .gz |
| 66 |
AddEncoding gzip .gz |
| 67 |
</IfModule> |
| 68 |
|
| 69 |
<IfModule mod_filter.c> |
| 70 |
AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon application/json application/vnd.ms-access video/webm application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint image/svg+xml application/x-shockwave-flash image/tiff application/x-font-ttf application/vnd.ms-opentype audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel |
| 71 |
|
| 72 |
<IfModule mod_mime.c> |
| 73 |
# DEFLATE by extension |
| 74 |
AddOutputFilter DEFLATE js css htm html xml |
| 75 |
</IfModule> |
| 76 |
</IfModule> |
| 77 |
|
| 78 |
RewriteCond %{HTTP:Accept-Encoding} gzip |
| 79 |
RewriteRule .* - [E=SWPSP_EXT:gz] |
| 80 |
</IfModule> |
| 81 |
|
| 82 |
# Fallback if the extension is gz but a gz file was never created, check for a .html extension |
| 83 |
RewriteCond %{ENV:SWPSP_EXT} =gz |
| 84 |
RewriteCond "%{DOCUMENT_ROOT}$base$cache_path/%{HTTP_HOST}/%{REQUEST_URI}/index%{ENV:SWPSP_SCHEME}%{ENV:SWPSP_DEVICE}.gz" !-f |
| 85 |
RewriteCond "$site_cache_path/%{REQUEST_URI}/index%{ENV:SWPSP_SCHEME}%{ENV:SWPSP_DEVICE}.gz" !-f |
| 86 |
RewriteRule .* - [E=SWPSP_EXT:html] |
| 87 |
|
| 88 |
# ---------------------------------- |
| 89 |
# Serve cache |
| 90 |
# ---------------------------------- |
| 91 |
RewriteCond %{REQUEST_METHOD} GET |
| 92 |
RewriteCond %{QUERY_STRING} ^$ |
| 93 |
RewriteCond %{REQUEST_FILENAME} !-f |
| 94 |
RewriteCond %{HTTP:Cookie} !(wordpress_logged_in_.+|wp-postpass_|wptouch_switch_toggle|comment_author_|comment_author_email_) [NC] |
| 95 |
RewriteCond %{REQUEST_URI} !^(/(?:.+/)?feed(?:/(?:.+/?)?)?$|/(?:.+/)?embed/|/(index.php/)?(.*)wp-json(/.*|$))$ [NC] |
| 96 |
RewriteCond "%{DOCUMENT_ROOT}$base$cache_path/%{HTTP_HOST}/%{REQUEST_URI}/index%{ENV:SWPSP_SCHEME}%{ENV:SWPSP_DEVICE}.%{ENV:SWPSP_EXT}" -f [OR] |
| 97 |
RewriteCond "$site_cache_path/%{REQUEST_URI}/index%{ENV:SWPSP_SCHEME}%{ENV:SWPSP_DEVICE}.%{ENV:SWPSP_EXT}" -f |
| 98 |
RewriteRule .* "$base$cache_path/%{HTTP_HOST}/%{REQUEST_URI}/index%{ENV:SWPSP_SCHEME}%{ENV:SWPSP_DEVICE}.%{ENV:SWPSP_EXT}" [L] |
| 99 |
</IfModule> |
| 100 |
|
| 101 |
# ---------------------------------- |
| 102 |
# Headers when cached file served |
| 103 |
# ---------------------------------- |
| 104 |
<IfModule mod_headers.c> |
| 105 |
<FilesMatch "index-(https|http)(-mobile)?\.($extensions_regex)$"> |
| 106 |
FileETag None |
| 107 |
Header unset ETag |
| 108 |
Header unset Pragma |
| 109 |
Header append Cache-Control "public" |
| 110 |
Header append Vary: Accept-Encoding |
| 111 |
Header always set X-Cached-By "Kadence Performance (htaccess)" |
| 112 |
Header set X-Cache "HIT (%{SWPSP_DEVICE_LABEL}e)" |
| 113 |
</FilesMatch> |
| 114 |
</IfModule> |
| 115 |
|
| 116 |
RULES; |
| 117 |
|
| 118 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, StellarWP.XSS.EscapeOutput.OutputNotEscaped |
| 119 |
echo apply_filters( 'solidwp/performance/htaccess/rules', $htaccess, $cache_path ); |
| 120 |
|