2011-07-17 7 views
27

Ai đó có thể cung cấp cho tôi cấu hình .htaccess được tối ưu hóa để xử lý nén, lưu trữ bộ nhớ đệm, proxy caching, v.v ... cho một trang web điển hình?Caching và nén gzip bằng htaccess

Ngoài khách truy cập của tôi, tôi cũng đang cố gắng làm cho Google PageSpeed ​​trở nên hạnh phúc. Tôi muốn sử dụng bộ nhớ đệm và nén gzip thông qua .htaccess hãy giúp tôi với mã của nó!

Tôi muốn bộ nhớ cache icon,pdf,flv,jpg,png,gif,js,css,swf file cho thời gian dài

Tôi muốn bộ nhớ cache xml,txt file cho 2 Days

Tôi muốn bộ nhớ cache html file cho 2 Days

Và tôi muốn nén html,txt,css,js,php tác phẩm của tôi bởi vì chúng có kích thước tệp lớn.

Có cách nào để gzip hình ảnh bằng cách sử dụng .htaccess không?

+1

Nén hình ảnh bằng 'gzip' không quan tâm nhiều vì GIF, JPEG hoặc PNG đã được định dạng nén. Nén chúng sẽ chỉ là một mất nguồn CPU trên máy chủ của bạn, mà không có bất kỳ lợi ích nào về băng thông. –

+0

@hakre: không có câu trả lời đúng cho tôi ngoại trừ anh chàng này! –

+1

Bạn có phải là @Hamid Seyyedi không? nếu bạn có, tài khoản người dùng này và [tài khoản khác của bạn] (http://stackoverflow.com/users/784971/hamid-seyyedi) cần phải được hợp nhất vào một tài khoản. Bạn không được phép có hai tài khoản riêng biệt. –

Trả lời

44
# 480 weeks 
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> 
Header set Cache-Control "max-age=290304000, public" 
</FilesMatch> 

# 2 DAYS 
<FilesMatch "\.(xml|txt)$"> 
Header set Cache-Control "max-age=172800, public, must-revalidate" 
</FilesMatch> 

# 2 HOURS 
<FilesMatch "\.(html|htm)$"> 
Header set Cache-Control "max-age=7200, must-revalidate" 
</FilesMatch> 

<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> 
+0

@hakre: không có câu trả lời đúng cho tôi ngoại trừ anh chàng này! Mở mắt ra! –

+2

Phần không hoạt động đối với tôi. Khi tôi thử nó trên trang web của riêng tôi, tôi đã gặp lỗi 500! Tuy nhiên phương thức AddOutputFilterByType trong câu trả lời bên dưới hoạt động hoàn hảo. Sự khác nhau giữa hai phương pháp này là gì? – maxxon15

+1

Apache 1.3 sử dụng [mod_gzip] (http://sourceforge.net/projects/mod-gzip/) trong khi Apache 2.x sử dụng [mod_deflate] (http://httpd.apache.org/docs/2.0/mod/mod_deflate .html). Vì vậy, hãy kiểm tra phiên bản apache của bạn, điều quan trọng của nó! – Andron

16

Hãy thử điều này:

<IfModule mod_deflate.c> 
    #The following line is enough for .js and .css 
    AddOutputFilter DEFLATE js css 
    AddOutputFilterByType DEFLATE text/plain text/xml application/xhtml+xml text/css application/xml application/rss+xml application/atom_xml application/x-javascript application/x-httpd-php application/x-httpd-fastphp text/html 

    #The following lines are to avoid bugs with some browsers 
    BrowserMatch ^Mozilla/4 gzip-only-text/html 
    BrowserMatch ^Mozilla/4\.0[678] no-gzip 
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
</IfModule> 

# BEGIN EXPIRES 
<IfModule mod_expires.c> 
    ExpiresActive On 
    ExpiresDefault "access plus 10 days" 
    ExpiresByType text/css "access plus 1 week" 
    ExpiresByType text/plain "access plus 1 month" 
    ExpiresByType image/gif "access plus 1 month" 
    ExpiresByType image/png "access plus 1 month" 
    ExpiresByType image/jpeg "access plus 1 month" 
    ExpiresByType application/x-javascript "access plus 1 month" 
    ExpiresByType application/javascript "access plus 1 week" 
    ExpiresByType application/x-icon "access plus 1 year" 
</IfModule> 
# END EXPIRES 

<IfModule mod_headers.c> 
    <FilesMatch "\.(js|css|xml|gz)$"> 
     Header append Vary Accept-Encoding 
    </FilesMatch> 
    <FilesMatch "\.(ico|jpe?g|png|gif|swf)$"> 
     Header set Cache-Control "public" 
    </FilesMatch> 
    <FilesMatch "\.(css)$"> 
     Header set Cache-Control "public" 
    </FilesMatch> 
    <FilesMatch "\.(js)$"> 
     Header set Cache-Control "private" 
    </FilesMatch> 
    <FilesMatch "\.(x?html?|php)$"> 
     Header set Cache-Control "private, must-revalidate" 
    </FilesMatch> 
</IfModule> 
+2

Trong apache 2.4, cả hai mô-đun phải được kích hoạt để sử dụng 'deflating': mod_deflate.so và mod_filter.so – Andron

2

Bạn có thể sử dụng các mã như: -

<Files *> 
    #Compress 

    SetOutputFilter GZIP 

    #Caching 

    Header set Cache-Control "max-age=3600, public" 


</Files> 
1

Bạn nên sử dụng quy tắc sau đây trong tập tin .htaccess của bạn tôi tin tưởng nó sẽ tăng tối thiểu 10 điểm nếu bạn chưa sử dụng.

 

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.* 


AddOutputFilterByType DEFLATE application/javascript 
AddOutputFilterByType DEFLATE application/rss+xml 
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject 
AddOutputFilterByType DEFLATE application/x-font 
AddOutputFilterByType DEFLATE application/x-font-opentype 
AddOutputFilterByType DEFLATE application/x-font-otf 
AddOutputFilterByType DEFLATE application/x-font-truetype 
AddOutputFilterByType DEFLATE application/x-font-ttf 
AddOutputFilterByType DEFLATE application/x-javascript 
AddOutputFilterByType DEFLATE application/xhtml+xml 
AddOutputFilterByType DEFLATE application/xml 
AddOutputFilterByType DEFLATE font/opentype 
AddOutputFilterByType DEFLATE font/otf 
AddOutputFilterByType DEFLATE font/ttf 
AddOutputFilterByType DEFLATE image/svg+xml 
AddOutputFilterByType DEFLATE image/x-icon 
AddOutputFilterByType DEFLATE text/css 
AddOutputFilterByType DEFLATE text/html 
AddOutputFilterByType DEFLATE text/javascript 
AddOutputFilterByType DEFLATE text/plain 
AddOutputFilterByType DEFLATE text/xml 
BrowserMatch ^Mozilla/4 gzip-only-text/html 
BrowserMatch ^Mozilla/4\.0[678] no-gzip 
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
Header append Vary User-Agent 


ExpiresActive On 
ExpiresByType image/jpg "access 1 year" 
ExpiresByType image/jpeg "access 1 year" 
ExpiresByType image/gif "access 1 year" 
ExpiresByType image/png "access 1 year" 
ExpiresByType text/css "access 1 month" 
ExpiresByType text/html "access 1 month" 
ExpiresByType application/pdf "access 1 month" 
ExpiresByType text/x-javascript "access 1 month" 
ExpiresByType application/x-shockwave-flash "access 1 month" 
ExpiresByType image/x-icon "access 1 year" 
ExpiresDefault "access 1 month" 

1

Enable nén qua .htaccess

Đối với hầu hết mọi người đọc bài viết này, nén được kích hoạt bằng cách thêm một số mã vào một tập tin gọi là .htaccess trên máy chủ/máy chủ web của họ. Điều này có nghĩa là đi đến trình quản lý tệp (hoặc bất cứ nơi nào bạn đi để thêm hoặc tải lên tệp) trên máy chủ web của bạn.

Tệp .htaccess kiểm soát nhiều điều quan trọng đối với trang web của bạn. Nếu bạn không quen thuộc với tệp .htaccess, hãy đọc bài viết của tôi với bài viết .htaccess để biết cách trước khi thay đổi nó.

Đoạn code dưới đây nên được thêm vào tập tin .htaccess bạn ...

<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> 

Lưu tập tin .htaccess và sau đó làm mới trang web của bạn.

Kiểm tra xem liệu quá trình nén của bạn có đang hoạt động không bằng cách sử dụng Gzip compression tool.