| 1 |
<?php |
| 2 |
|
| 3 |
// Check if the server is Apache |
| 4 |
if (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) { |
| 5 |
|
| 6 |
// Get the path to the .htaccess file |
| 7 |
$htaccessFile = ABSPATH . '.htaccess'; |
| 8 |
|
| 9 |
// Check if the file exists |
| 10 |
if (file_exists($htaccessFile)) { |
| 11 |
|
| 12 |
// Get the current content of the .htaccess file |
| 13 |
$currentContent = file_get_contents($htaccessFile); |
| 14 |
|
| 15 |
// Check if caching rules are already present |
| 16 |
if (strpos($currentContent, '# BerqWP Caching Rules') === false) { |
| 17 |
|
| 18 |
// Append caching rules to the .htaccess file |
| 19 |
$newRules = <<<EOD |
| 20 |
|
| 21 |
# BerqWP Caching Rules |
| 22 |
<IfModule mod_expires.c> |
| 23 |
ExpiresActive On |
| 24 |
ExpiresByType image/jpg "access 1 year" |
| 25 |
ExpiresByType image/webp "access 1 year" |
| 26 |
ExpiresByType image/jpeg "access 1 year" |
| 27 |
ExpiresByType image/gif "access 1 year" |
| 28 |
ExpiresByType image/png "access 1 year" |
| 29 |
ExpiresByType image/svg "access 1 year" |
| 30 |
ExpiresByType text/css "access 1 month" |
| 31 |
ExpiresByType application/pdf "access 1 month" |
| 32 |
ExpiresByType application/javascript "access 1 month" |
| 33 |
ExpiresByType application/x-javascript "access 1 month" |
| 34 |
ExpiresByType application/x-shockwave-flash "access 1 month" |
| 35 |
ExpiresByType image/x-icon "access 1 year" |
| 36 |
ExpiresDefault "access 3 days" |
| 37 |
</IfModule> |
| 38 |
|
| 39 |
<filesMatch ".(ico|pdf|flv|jpg|jpeg|webp|png|gif|svg|js|css|woff|woff2|ttf|swf)$"> |
| 40 |
Header set Cache-Control "max-age=96000, public" |
| 41 |
</filesMatch> |
| 42 |
|
| 43 |
<FilesMatch "\.(js|css|html|xml|txt)$"> |
| 44 |
SetOutputFilter DEFLATE |
| 45 |
</FilesMatch> |
| 46 |
|
| 47 |
FilesMatch "\.(pdf|doc|avi|mp4|zip)$"> |
| 48 |
Header set Cache-Control "max-age=604800, public" |
| 49 |
</FilesMatch> |
| 50 |
# BerqWP Caching Rules Ends Here |
| 51 |
EOD; |
| 52 |
|
| 53 |
// Append the new rules to the .htaccess file |
| 54 |
file_put_contents($htaccessFile, $currentContent . $newRules); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
} |
| 61 |
|