| 1 |
<?php |
| 2 |
|
| 3 |
include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-rewrite-rules-abstract.php'; |
| 4 |
|
| 5 |
class Apache extends Rewrite_Rules_Abstract { |
| 6 |
|
| 7 |
/** |
| 8 |
* Get the path to the file. |
| 9 |
*/ |
| 10 |
protected function get_file_path() { |
| 11 |
$file_path = $this->get_site_root() . '.htaccess'; |
| 12 |
|
| 13 |
return $file_path; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Get unfiltered new contents to write into the file. |
| 18 |
*/ |
| 19 |
protected function get_raw_new_contents() { |
| 20 |
$extensions = 'jpg|jpeg|jpe|png'; |
| 21 |
$home_root = wp_parse_url( home_url( '/' ) ); |
| 22 |
$home_root = $home_root['path']; |
| 23 |
|
| 24 |
return trim( ' |
| 25 |
# BEGIN ' . $this->tag_name . ' |
| 26 |
<IfModule mod_setenvif.c> |
| 27 |
# Vary: Accept for all the requests to jpeg and png. |
| 28 |
SetEnvIf Request_URI "\.(' . $extensions . ')$" REQUEST_image |
| 29 |
</IfModule> |
| 30 |
|
| 31 |
<IfModule mod_rewrite.c> |
| 32 |
RewriteEngine On |
| 33 |
RewriteBase ' . $home_root . ' |
| 34 |
|
| 35 |
# Check if browser supports WebP images. |
| 36 |
RewriteCond %{HTTP_ACCEPT} image/webp |
| 37 |
|
| 38 |
# Check if WebP replacement image exists. |
| 39 |
RewriteCond %{REQUEST_FILENAME}.webp -f |
| 40 |
|
| 41 |
# Serve WebP image instead. |
| 42 |
RewriteRule (.+)\.(' . $extensions . ')$ $1.$2.webp [T=image/webp,NC] |
| 43 |
</IfModule> |
| 44 |
|
| 45 |
<IfModule mod_headers.c> |
| 46 |
Header append Vary Accept env=REQUEST_image |
| 47 |
</IfModule> |
| 48 |
|
| 49 |
<IfModule mod_mime.c> |
| 50 |
AddType image/webp .webp |
| 51 |
</IfModule> |
| 52 |
# END ' . $this->tag_name .'' ); |
| 53 |
} |
| 54 |
|
| 55 |
} |