{"id":19699,"date":"2025-08-27T02:34:48","date_gmt":"2025-08-27T05:34:48","guid":{"rendered":"https:\/\/king.host\/wiki\/?post_type=article&#038;p=19699"},"modified":"2025-08-27T02:34:56","modified_gmt":"2025-08-27T05:34:56","slug":"controle-de-cache","status":"publish","type":"article","link":"https:\/\/king.host\/wiki\/artigo\/controle-de-cache\/","title":{"rendered":"Controle de Cache com uso de regras Expires \/ max-age no .htaccess"},"content":{"rendered":"\n<p><em>Gostaria de controlar o tempo de Cache dos seus arquivos, com o intuito de otimizar a performance e o ranqueamento nos motores de busca? Veja neste artigo<\/em> <em>como ter o controle atrav\u00e9s do arquivo<strong> .htaccess<\/strong><\/em>.<\/p>\n\n\n\n<p>A utiliza\u00e7\u00e3o de Cache nos websites \u00e9 muito importante para acelerar o carregamento do conte\u00fado, al\u00e9m de te ajudar na otimiza\u00e7\u00e3o do ranqueamento nos principais motores de busca da internet, como o Google.<\/p>\n\n\n\n<p>Sites como o <a rel=\"noreferrer noopener\" aria-label=\" (abre numa nova aba)\" href=\"https:\/\/gtmetrix.com\/\" target=\"_blank\">GTmetrix<\/a> analisam e d\u00e3o nota para a performance de um site. Um dos requisitos desta an\u00e1lise \u00e9 o <strong>controle de cache<\/strong>, no qual o <em>GTmetrix<\/em> recomenda que seja configurado o per\u00edodo de <strong>1 ano<\/strong> para arquivos do tipo <strong>imagem<\/strong> e <strong>script<\/strong>. Veja abaixo:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/king.host\/wiki\/wp-content\/uploads\/2019\/11\/cache01.png\" v-on:click=\"toggle_modal\"><img data-original=\"https:\/\/king.host\/wiki\/wp-content\/uploads\/2019\/11\/cache01.png\" alt=\"\" class=\"wp-image-19702\"\/><\/a><\/figure>\n\n\n\n<p> Caso seu site esteja em ambiente <strong>Linux<\/strong>, \u00e9 poss\u00edvel realizar essas configura\u00e7\u00f5es atrav\u00e9s do arquivo <strong>.htaccess<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Certo_mas_o_que_e_o_arquivo_htaccess\"><\/span>Certo, mas o que \u00e9 o arquivo .htaccess?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>O<strong> .htaccess<\/strong> \u00e9 basicamente um arquivo oculto, utilizado para servidores web baseados em <strong>Apache<\/strong>, que serve para defini\u00e7\u00e3o de algumas configura\u00e7\u00f5es iniciais de seu site. Voc\u00ea pode, por exemplo:<\/p>\n\n\n\n<ul>\n<li>Proteger seu site com uma senha;<\/li>\n\n\n\n<li>Criar uma p\u00e1gina de erro customizada;<\/li>\n\n\n\n<li>Redirecionar visitantes para outra p\u00e1gina.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Controle_de_Cache_com_uso_de_regras_Expires_max-age_no_htaccess\"><\/span>Controle de Cache com uso de regras Expires \/ max-age no .htaccess<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Primeiramente, \u00e9 necess\u00e1rio que voc\u00ea tenha acesso ao FTP de seu site. Caso n\u00e3o saiba como acessar, veja <a rel=\"noreferrer noopener\" aria-label=\" (abre numa nova aba)\" href=\"https:\/\/king.host\/wiki\/artigo\/acessar-ftp-via-filezilla\/\" target=\"_blank\">este artigo<\/a>.<\/p>\n\n\n\n<p>Em seu arquivo <strong>.htaccess<\/strong>, voc\u00ea ir\u00e1 configurar o tempo de cache atrav\u00e9s do <strong>Expires<\/strong>, conforme exemplo abaixo:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\" lang=\"wp-block-preformatted\">ExpiresActive On\nExpiresByType image\/jpg \"access 1 year\"\nExpiresByType image\/jpeg \"access 1 year\"\nExpiresByType image\/gif \"access 1 year\"\nExpiresByType image\/png \"access 1 year\"\nExpiresByType text\/css \"access 1 month\"\nExpiresByType text\/html \"access 1 month\"\nExpiresByType application\/pdf \"access 1 month\"\nExpiresByType text\/x-javascript \"access 1 month\"\nExpiresByType application\/x-shockwave-flash \"access 1 month\"\nExpiresByType image\/x-icon \"access 1 year\"\nExpiresDefault \"access 1 month\"<\/pre>\n\n\n\n<p>Contudo, esta regra pode n\u00e3o funcionar corretamente, pois a diretiva &#8220;<strong>Max-Age<\/strong>&#8221; possui <a rel=\"noreferrer noopener\" aria-label=\" (abre numa nova aba)\" href=\"https:\/\/developer.mozilla.org\/pt-BR\/docs\/Web\/HTTP\/Headers\/Set-Cookie\" target=\"_blank\">preced\u00eancia<\/a> sobre o &#8220;<strong>Expires<\/strong>&#8220;. <\/p>\n\n\n\n<p>No nosso servidor web, o &#8220;<strong>Max-Age<\/strong>&#8221; j\u00e1 est\u00e1 configurado por padr\u00e3o para <strong>1800 segundos (30 minutos)<\/strong>, por\u00e9m esta regra pode ser sobrescrita pelo pr\u00f3prio arquivo .htaccess.<\/p>\n\n\n\n<p>Para resolver isso, basta inserir a seguinte regra abaixo, no final do arquivo .htaccess:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\" lang=\"wp-block-preformatted\">&lt;IfModule mod_headers.c&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;FilesMatch \"\\.(ico|gif|jpg|jpeg|png)$\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Header set Cache-Control \"max-age=31536000\"\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/FilesMatch&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;FilesMatch \"\\.(css|js)$\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Header set Cache-Control \"max-age=31536000\"\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/FilesMatch&gt;\n&lt;\/IfModule&gt;<\/pre>\n\n\n\n<p>Na pr\u00f3xima imagem, vemos que o tempo de cache j\u00e1 est\u00e1 definido para 1 ano:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/king.host\/wiki\/wp-content\/uploads\/2019\/11\/cache02.png\" v-on:click=\"toggle_modal\"><img loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"307\" data-src=\"https:\/\/king.host\/wiki\/wp-content\/uploads\/2019\/11\/cache02.png\" alt=\"\" class=\"wp-image-19707 lazyload\" data-srcset=\"https:\/\/king.host\/wiki\/wp-content\/uploads\/2019\/11\/cache02.png 450w, https:\/\/king.host\/wiki\/wp-content\/uploads\/2019\/11\/cache02-300x205.png 300w\" data-sizes=\"(max-width: 450px) 100vw, 450px\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" style=\"--smush-placeholder-width: 450px; --smush-placeholder-aspect-ratio: 450\/307;\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"307\" src=\"https:\/\/king.host\/wiki\/wp-content\/uploads\/2019\/11\/cache02.png\" alt=\"\" class=\"wp-image-19707\" srcset=\"https:\/\/king.host\/wiki\/wp-content\/uploads\/2019\/11\/cache02.png 450w, https:\/\/king.host\/wiki\/wp-content\/uploads\/2019\/11\/cache02-300x205.png 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/noscript><\/a><\/figure>\n\n\n\n<box-info class=\"wp-block-k-box-info\">\ud83d\udca1&nbsp;max-age=31536000 \u00e9 equivalente a <strong>1 ano<\/strong>.<\/box-info>\n","protected":false},"author":9,"featured_media":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","article-category":[31],"article-tag":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/king.host\/wiki\/wp-json\/wp\/v2\/article\/19699"}],"collection":[{"href":"https:\/\/king.host\/wiki\/wp-json\/wp\/v2\/article"}],"about":[{"href":"https:\/\/king.host\/wiki\/wp-json\/wp\/v2\/types\/article"}],"author":[{"embeddable":true,"href":"https:\/\/king.host\/wiki\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/king.host\/wiki\/wp-json\/wp\/v2\/comments?post=19699"}],"version-history":[{"count":1,"href":"https:\/\/king.host\/wiki\/wp-json\/wp\/v2\/article\/19699\/revisions"}],"predecessor-version":[{"id":26937,"href":"https:\/\/king.host\/wiki\/wp-json\/wp\/v2\/article\/19699\/revisions\/26937"}],"wp:attachment":[{"href":"https:\/\/king.host\/wiki\/wp-json\/wp\/v2\/media?parent=19699"}],"wp:term":[{"taxonomy":"article-category","embeddable":true,"href":"https:\/\/king.host\/wiki\/wp-json\/wp\/v2\/article-category?post=19699"},{"taxonomy":"article-tag","embeddable":true,"href":"https:\/\/king.host\/wiki\/wp-json\/wp\/v2\/article-tag?post=19699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}