Сжатие Gzip и использование кэша браузера Не работает в моем магазине


Я пытаюсь оптимизировать свой магазин, я добавил код Gzip и использую код кэша браузера в своем файле .htaccess:

# Leverage browser caching using mod_expires #
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 2 days"
</IfModule>
# End of Leverage browser caching using mod_expires #

# Leverage browser caching using mod_headers #
<IfModule mod_headers.c>
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
        Header set Expires "Wed, 15 Apr 2020 20:00:00 GMT"
        Header set Cache-Control "public"
    </FilesMatch>
</IfModule>
# End of Leverage browser caching using mod_headers #
Author: Joe, 2017-07-28

2 answers

Включить модули Apache mod_headers и mod_expires

Пожалуйста, проверьте, включены ли mod_expires и mod_headers в вашем магазине или нет, с помощью приведенного ниже кода.

<?php phpinfo();?>

Если оба расширения не могут подключиться к вашему серверу, пожалуйста, выполните следующие действия: -

Шаг 1 :-

Теперь войдите на сервер с помощью консоли SSH и выполните следующие действия:

Чтобы включить заголовки модов:

sudo a2enmod headers

Чтобы включить mod_expires:

sudo a2enmod expires

Шаг 2 :-

После завершения обновления сервера вам необходимо перезапустить сервер Apache чтобы сделать эти изменения эффективными. Введите нижеприведенную строку в консоли SSH , чтобы перезапустить Apache.

service apache2 restart

Или же перезагрузите свой сервер, выполнив следующий код в SSH:

reboot

Используйте следующий код в файле .htaccess.

<IfModule mod_headers.c>
     # YEAR
     <FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf)$">
          Header set Cache-Control "max-age=29030400"
     </FilesMatch>
     # WEEK
     <FilesMatch "\.(js|css|swf|woff)$">
         Header set Cache-Control "max-age=604800"
     </FilesMatch>
     # 45 MIN
     <FilesMatch "\.(html|htm|txt)$">
        Header set Cache-Control "max-age=86400"
     </FilesMatch>

     Header set Connection keep-alive

</IfModule>

<ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl|asp|html)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>

После очистки вашего магазина кэшируйте и перезагрузите URL-адрес своего веб-сайта, перепроверьте инструмент gtmetrix или любой другой инструмент, который вы используете.

 5
Author: Umesh Kumar, 2017-07-28 09:29:44

Вот код для сжатия GZIP

<ifModule mod_deflate.c>

   SetOutputFilter DEFLATE
   AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/x-httpd-php
   BrowserMatch ^Mozilla/4 gzip-only-text/html
   BrowserMatch ^Mozilla/4\.0[678] no-gzip
   BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
   BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
   SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip
   Header append Vary User-Agent env=!dont-vary
</ifModule>

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

И для использования кэширования браузера ваш код верен, но если он не работает, вам нужно проверить, включены ли модули mod_expires и mod_headers на вашем сервере или нет.

 0
Author: khushi, 2017-07-28 09:14:29