.htaccess를 통해 브라우저 캐싱을 활용하여 WordPress 속도를 높이는 방법
게시 됨: 2017-08-04브라우저 캐싱을 활용하여 웹 페이지를 더 빠르게 만드십시오. 브라우저 캐싱을 활용할 수 있다면 웹사이트 속도를 상당히 높일 수 있습니다. Google이 사이트 속도를 SEO 매개변수로 고려하기 시작함에 따라 웹마스터는 브라우저 캐싱을 활용하여 사이트 속도를 개선하고 더 나은 검색 엔진 순위를 얻을 수 있습니다.
다음은 Crunchify 루트 폴더에 있는 완전한 .htaccess
파일입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
######### CRUNCHIFY SETTING - START ########## Options All - Indexes # Disable ETags < IfModule mod_headers . c > Header unset ETag Header set Connection keep - alive < / IfModule > FileETag None ############## MaxCDN Fix ############# < IfModule mod_headers . c > < FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$" > Header set Access - Control - Allow - Origin "*" < / FilesMatch > < / IfModule > ########### REDIRECT TRAFFIC TO HTTPS ############ # RewriteEngine On # RewriteCond %{HTTPS} off # RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] ############ SECURITY ########### < FilesMatch "\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$" > Order allow , deny Deny from all < / FilesMatch > < Files robots . txt > Allow from all < / Files > ############## CACHING-GZIP ############ < IfModule mod_expires . c > ExpiresActive On ExpiresDefault A2592000 < FilesMatch "\.(txt|xml|js)$" > ExpiresDefault A2592000 < / FilesMatch > < FilesMatch "\.(css)$" > ExpiresDefault A2592000 < / FilesMatch > < FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$" > ExpiresDefault A2592000 < / FilesMatch > < FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$" > ExpiresDefault A2592000 < / FilesMatch > < / IfModule > < IfModule mod_headers . c > < FilesMatch "\.(txt|xml|js)$" > Header set Cache - Control "max-age=2592000" < / FilesMatch > < FilesMatch "\.(css)$" > Header set Cache - Control "max-age=2592000" < / FilesMatch > < FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$" > Header set Cache - Control "max-age=2592000" < / FilesMatch > < FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$" > Header set Cache - Control "max-age=2592000" < / FilesMatch > < / IfModule > < IfModule mod_deflate . c > < IfModule mod_setenvif . c > < IfModule mod_headers . c > SetEnvIfNoCase ^ ( Accept - EncodXng | X - cept - Encoding | X { 15 } | ~ { 15 } | - { 15 } ) $ ^ ( ( gzip | deflate ) \ s* , ? \ s* ) + | [ X ~ - ] { 4 , 13 } $ HAVE_Accept - Encoding RequestHeader append Accept - Encoding "gzip,deflate" env = HAVE_Accept - Encoding < / IfModule > < / IfModule > < IfModule mod_filter . c > AddOutputFilterByType DEFLATE "application/atom+xml" \ "application/javascript" \ "application/json" \ "application/ld+json" \ "application/manifest+json" \ "application/rdf+xml" \ "application/rss+xml" \ "application/schema+json" \ "application/vnd.geo+json" \ "application/vnd.ms-fontobject" \ "application/x-font-ttf" \ "application/x-javascript" \ "application/x-web-app-manifest+json" \ "application/xhtml+xml" \ "application/xml" \ "font/eot" \ "font/opentype" \ "image/bmp" \ "image/svg+xml" \ "image/vnd.microsoft.icon" \ "image/x-icon" \ "text/cache-manifest" \ "text/css" \ "text/html" \ "text/javascript" \ "text/plain" \ "text/vcard" \ "text/vnd.rim.location.xloc" \ "text/vtt" \ "text/x-component" \ "text/x-cross-domain-policy" \ "text/xml" < / IfModule > < IfModule mod_mime . c > AddEncoding gzip svgz < / IfModule > < / IfModule > ######### CRUNCHIFY SETTING END ############ # BEGIN WordPress < IfModule mod_rewrite . c > RewriteEngine On RewriteBase / RewriteRule ^ index \ . php $ - [ L ] RewriteCond % { REQUEST_FILENAME } ! - f RewriteCond % { REQUEST_FILENAME } ! - d RewriteRule . / index . php [ L ] < / IfModule > # END WordPress |

참고 : HTTPS를 사용하지 않는 경우 HTTPS 차단으로 리디렉션을 제거하세요. 나는 이미 그것을 주석 처리했습니다. 사이트에서 이미 HTTPS를 활성화했고 여전히 사용자가 HTTP를 통해 사이트를 방문하도록 허용하고 있다면 문제가 없습니다.
.htaccess 파일의 각 섹션을 이해합시다.
1단계 ETag 제거
우선 Expires 기간을 사용할 것이기 때문에 disable ETag header
해야 합니다. ETag
기술은 느리고 문제가 있는 것으로 알려져 있습니다. 다른 상위 사이트에서도 이에 대해 불평합니다.
.htaccess
에 추가: (블로그의 루트 위치에 있음)
1 2 3 4 5 6 |
# Disable ETags < IfModule mod_headers . c > Header unset ETag Header set Connection keep - alive < / IfModule > FileETag None |
우리는 또한 연결 keep-alive
를 유지하고 있습니다. persistent connection
이라고 합니다. 모든 요청이나 파일에 대해 새 연결을 열어야 하는 경우 시간이 훨씬 더 오래 걸릴 수 있습니다.
기타 반드시 읽어야 할 사항:
- WordPress 속도를 높이고 성능을 높이는 5가지 유용한 트릭
- 놓쳤을 수 있는 9가지 필수 WordPress 리소스
2단계 브라우저 캐싱 활성화
정적 리소스의 HTTP 헤더에 만료 날짜 또는 최대 수명을 설정하면 최신 브라우저는 네트워크가 아닌 로컬 디스크에서 이미지, CSS, 자바스크립트, pdf, swf 등과 같은 이전에 다운로드한 정적 리소스를 로드합니다.
따라서 캐싱 헤더를 설정하고 캐시 가능한 모든 정적 리소스에 적용하도록 웹 서버를 구성하면 사이트가 훨씬 빠르게 로드되는 것처럼 보입니다. .htaccess 에 아래 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
< IfModule mod_expires . c > ExpiresActive On ExpiresDefault A2592000 < FilesMatch "\.(txt|xml|js)$" > ExpiresDefault A2592000 < / FilesMatch > < FilesMatch "\.(css)$" > ExpiresDefault A2592000 < / FilesMatch > < FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$" > ExpiresDefault A2592000 < / FilesMatch > < FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$" > ExpiresDefault A2592000 < / FilesMatch > < / IfModule > < IfModule mod_headers . c > < FilesMatch "\.(txt|xml|js)$" > Header set Cache - Control "max-age=2592000" < / FilesMatch > < FilesMatch "\.(css)$" > Header set Cache - Control "max-age=2592000" < / FilesMatch > < FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$" > Header set Cache - Control "max-age=2592000" < / FilesMatch > < FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$" > Header set Cache - Control "max-age=2592000" < / FilesMatch > < / IfModule > |
이것이 하는 일은 정적 콘텐츠(이미지, js, CSS 등)에 먼 미래 만료 헤더(문제가 있는 경우 아파치 구성에 mod_expires
가 로드되었는지 확인)를 추가하는 것입니다.
여기 두 가지:
- ExpiresDefault A2592000 = 향후 1개월
- 캐시 제어 "max-age=2592000" = 1개월
원하는 경우 값을 1 Year = 31536000으로 설정할 수도 있습니다.
3단계 gzip 추가 및 압축 헤더 수축
압축하면 항상 크기가 작아지고 로드 속도가 빨라지므로 구성 요소에 압축을 구현하는 것이 필수입니다.
서버에 Apache의 일부로 mod_deflate
또는 mod_gzip
이 설치되어 있지 않으면 이 최적화 단계가 작동하지 않을 수 있습니다.
기본적으로 우리는 더 적은 대역폭으로 매우 빠르게 로드되도록 대부분의 리소스를 압축하고 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
< IfModule mod_deflate . c > < IfModule mod_setenvif . c > < IfModule mod_headers . c > SetEnvIfNoCase ^ ( Accept - EncodXng | X - cept - Encoding | X { 15 } | ~ { 15 } | - { 15 } ) $ ^ ( ( gzip | deflate ) \ s* , ? \ s* ) + | [ X ~ - ] { 4 , 13 } $ HAVE_Accept - Encoding RequestHeader append Accept - Encoding "gzip,deflate" env = HAVE_Accept - Encoding < / IfModule > < / IfModule > < IfModule mod_filter . c > AddOutputFilterByType DEFLATE "application/atom+xml" \ "application/javascript" \ "application/json" \ "application/ld+json" \ "application/manifest+json" \ "application/rdf+xml" \ "application/rss+xml" \ "application/schema+json" \ "application/vnd.geo+json" \ "application/vnd.ms-fontobject" \ "application/x-font-ttf" \ "application/x-javascript" \ "application/x-web-app-manifest+json" \ "application/xhtml+xml" \ "application/xml" \ "font/eot" \ "font/opentype" \ "image/bmp" \ "image/svg+xml" \ "image/vnd.microsoft.icon" \ "image/x-icon" \ "text/cache-manifest" \ "text/css" \ "text/html" \ "text/javascript" \ "text/plain" \ "text/vcard" \ "text/vnd.rim.location.xloc" \ "text/vtt" \ "text/x-component" \ "text/x-cross-domain-policy" \ "text/xml" < / IfModule > < IfModule mod_mime . c > AddEncoding gzip svgz < / IfModule > < / IfModule > |
4단계 설정이 올바르게 작동하는지 확인
아래 스크린샷을 확인하세요. 파일의 응답 헤더에 설정된 모든 매개변수가 표시되어야 합니다.
열린 질문: Google Adsense 스크립트 또는 Google Analytics 스크립트와 같은 일부 리소스의 만료 날짜를 변경할 수 있습니까?
- https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js
- https://www.google-analytics.com/analytics.js
Answer is NO
입니다. 사이트에서 로드되는 리소스의 만료 값만 설정할 수 있습니다.