1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Use htaccess to speed up your website

Discussion in 'Apache' started by scylla, Jun 19, 2012.

  1. #1
    I have made this guide because in the past I have seen so many other examples of "htaccess files that work" when in reality they do not.
    Therefor, I have decided to have a look at w3tc's htaccess that it produces and quickly found that it can be applied to much much more than just wordpress.
    Applying the following htaccess will cache your static files and it will automatically handle gzipping. For example, in this vbulletin forum here... in the admin cp I have the gzip compression in there TURNED OFF. Yet my resources are perfectly gzipped according to yslow, pagespeed, gtmetrix & webpagetest.
    Before you apply the htaccess below, I STRONGLY recommend that you make a backup of your htaccess just incase your host does not have mod gzip or mod deflate enabled; two apache modules that are vital to caching & gzipping.

    I decided to make this thread after I made my original step by step guide on how to speed up vbulletin at vbulletin.org and after I decided to release it to the public on my site, vgchat.

    There are several steps to improve your page loading speed. I will go over a few steps that will improve your site's loading time.

    Step 1: Tweak your htaccess.
    Open the htaccess file in your root and add the following below any url rewrite rules you may have:

    Doing this covers the following Pagespeed & Yslow Rules.

    Leverage browser caching
    Specify a cache validator
    Enable gzip compression


    Code (Text):
    1. # Author: Final Kaoss (aka: Extreme-Gaming)
    2. # Version 2.6
    3. RewriteEngine on
    4.  
    5. #Deny attempts to view the Htaccess file.
    6. <Files .htaccess>
    7. Order allow,deny
    8. Deny from all
    9. </Files>
    10. # BEGIN Browser Caching/Headers
    11. <IfModule mod_mime.c>
    12.     AddType text/css .css
    13.     AddType text/richtext .rtf .rtx
    14.     AddType image/svg+xml .svg .svgz
    15.     AddType text/plain .txt
    16.     AddType text/xsd .xsd
    17.     AddType text/xsl .xsl
    18.     AddType video/asf .asf .asx .wax .wmv .wmx
    19.     AddType video/avi .avi
    20.     AddType image/bmp .bmp
    21.     AddType application/java .class
    22.     AddType video/divx .divx
    23.     AddType application/msword .doc .docx
    24.     AddType application/x-msdownload .exe
    25.     AddType image/gif .gif
    26.     AddType application/x-gzip .gz .gzip
    27.     AddType image/x-icon .ico
    28.     AddType image/jpeg .jpg .jpeg .jpe
    29.     AddType application/vnd.ms-access .mdb
    30.     AddType audio/midi .mid .midi
    31.     AddType video/quicktime .mov .qt
    32.     AddType audio/mpeg .mp3 .m4a
    33.     AddType video/mp4 .mp4 .m4v
    34.     AddType video/mpeg .mpeg .mpg .mpe
    35.     AddType application/vnd.ms-project .mpp
    36.     AddType application/vnd.oasis.opendocument.database .odb
    37.     AddType application/vnd.oasis.opendocument.chart .odc
    38.     AddType application/vnd.oasis.opendocument.formula .odf
    39.     AddType application/vnd.oasis.opendocument.graphics .odg
    40.     AddType application/vnd.oasis.opendocument.presentation .odp
    41.     AddType application/vnd.oasis.opendocument.spreadsheet .ods
    42.     AddType application/vnd.oasis.opendocument.text .odt
    43.     AddType audio/ogg .ogg
    44.     AddType application/pdf .pdf
    45.     AddType image/png .png
    46.     AddType application/vnd.ms-powerpoint .pot .pps .ppt .pptx
    47.     AddType audio/x-realaudio .ra .ram
    48.     AddType application/x-shockwave-flash .swf
    49.     AddType application/x-tar .tar
    50.     AddType image/tiff .tif .tiff
    51.     AddType audio/wav .wav
    52.     AddType audio/wma .wma
    53.     AddType application/vnd.ms-write .wri
    54.     AddType application/vnd.ms-excel .xla .xls .xlsx .xlt .xlw
    55.     AddType application/zip .zip
    56. </IfModule>
    57. <IfModule mod_expires.c>
    58.     ExpiresActive On
    59.     ExpiresByType text/css A2628000
    60.     ExpiresByType text/richtext A3600
    61.     ExpiresByType image/svg+xml A3600
    62.     ExpiresByType text/plain A3600
    63.     ExpiresByType text/xsd A3600
    64.     ExpiresByType text/xsl A3600
    65.     ExpiresByType video/asf A2628000
    66.     ExpiresByType video/avi A2628000
    67.     ExpiresByType image/bmp A2628000
    68.     ExpiresByType application/java A2628000
    69.     ExpiresByType video/divx A2628000
    70.     ExpiresByType application/msword A2628000
    71.     ExpiresByType application/x-msdownload A2628000
    72.     ExpiresByType image/gif A2628000
    73.     ExpiresByType application/x-gzip A2628000
    74.     ExpiresByType image/x-icon A2628000
    75.     ExpiresByType image/jpeg A2628000
    76.     ExpiresByType application/vnd.ms-access A2628000
    77.     ExpiresByType audio/midi A2628000
    78.     ExpiresByType video/quicktime A2628000
    79.     ExpiresByType audio/mpeg A2628000
    80.     ExpiresByType video/mp4 A2628000
    81.     ExpiresByType video/mpeg A2628000
    82.     ExpiresByType application/vnd.ms-project A2628000
    83.     ExpiresByType application/vnd.oasis.opendocument.database A2628000
    84.     ExpiresByType application/vnd.oasis.opendocument.chart A2628000
    85.     ExpiresByType application/vnd.oasis.opendocument.formula A2628000
    86.     ExpiresByType application/vnd.oasis.opendocument.graphics A2628000
    87.     ExpiresByType application/vnd.oasis.opendocument.presentation A2628000
    88.     ExpiresByType application/vnd.oasis.opendocument.spreadsheet A2628000
    89.     ExpiresByType application/vnd.oasis.opendocument.text A2628000
    90.     ExpiresByType audio/ogg A2628000
    91.     ExpiresByType application/pdf A2628000
    92.     ExpiresByType image/png A2628000
    93.     ExpiresByType application/vnd.ms-powerpoint A2628000
    94.     ExpiresByType audio/x-realaudio A2628000
    95.     ExpiresByType application/x-shockwave-flash A2628000
    96.     ExpiresByType application/x-tar A2628000
    97.     ExpiresByType image/tiff A2628000
    98.     ExpiresByType audio/wav A2628000
    99.     ExpiresByType audio/wma A2628000
    100.     ExpiresByType application/vnd.ms-write A2628000
    101.     ExpiresByType application/vnd.ms-excel A2628000
    102.     ExpiresByType application/zip A2628000
    103. </IfModule>
    104. <IfModule mod_deflate.c>
    105.     <IfModule mod_setenvif.c>
    106.         BrowserMatch ^Mozilla/4 gzip-only-text/html
    107.         BrowserMatch ^Mozilla/4\.0[678] no-gzip
    108.         BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    109.         BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
    110.     </IfModule>
    111.     <IfModule mod_headers.c>
    112.         Header append Vary User-Agent env=!dont-vary
    113.     </IfModule>
    114.     <IfModule mod_filter.c>
    115.         AddOutputFilterByType DEFLATE text/css application/x-javascript text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon
    116.     </IfModule>
    117. </IfModule>
    118. <FilesMatch "\.(css|CSS|)$">
    119.     <IfModule mod_headers.c>
    120.         Header set Pragma "public"
    121.         Header append Cache-Control "public, must-revalidate, proxy-revalidate"
    122.     </IfModule>
    123.     FileETag MTime Size
    124.     <IfModule mod_headers.c>
    125.          Header set X-Powered-By "Step by Step guide to speed up your VB4 (forum only)"
    126.     </IfModule>
    127. </FilesMatch>
    128. <FilesMatch "\.(rtf|rtx|svg|svgz|txt|xsd|xsl|RTF|RTX|SVG|SVGZ|TXT|XSD|XSL|)$">
    129.     <IfModule mod_headers.c>
    130.         Header set Pragma "public"
    131.         Header append Cache-Control "public, must-revalidate, proxy-revalidate"
    132.     </IfModule>
    133.     FileETag MTime Size
    134.     <IfModule mod_headers.c>
    135.          Header set X-Powered-By "Step by Step guide to speed up your VB4 (forum only)"
    136.     </IfModule>
    137. </FilesMatch>
    138. <FilesMatch "\.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip|ASF|ASX|WAX|WMV|WMX|AVI|BMP|CLASS|DIVX|DOC|DOCX|EXE|GIF|GZ|GZIP|ICO|JPG|JPEG|JPE|MDB|MID|MIDI|MOV|QT|MP3|M4A|MP4|M4V|MPEG|MPG|MPE|MPP|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|PDF|PNG|POT|PPS|PPT|PPTX|RA|RAM|SWF|TAR|TIF|TIFF|WAV|WMA|WRI|XLA|XLS|XLSX|XLT|XLW|ZIP)$">
    139.     <IfModule mod_headers.c>
    140.         Header set Pragma "public"
    141.         Header append Cache-Control "public, must-revalidate, proxy-revalidate"
    142.     </IfModule>
    143.     FileETag MTime Size
    144.     <IfModule mod_headers.c>
    145.          Header set X-Powered-By "Step by Step guide to speed up your VB4 (forum only)"
    146.     </IfModule>
    147. </FilesMatch>
    148. # End Browser Caching/Headers
    You can check your results at these two sites.

    http://gtmetrix.com/dashboard.html
    http://www.webpagetest.org/

    If you fail caching or gzip rules for your local pages (ignore external resources on these tests!) then you need to find a new host, quite simply.


    Additionally, you can use the htaccess above for virtually any website. It does not matter if you are running a website that you built by hand that uses html or php, phpbb, vbulletin, mybb, invision, xenforo, smf, wordpress, joomla, phpnuke or any other CMS that you can think of. If your software has "seo friendly urls", you may add those rules into the htaccess file provided and they will work just fine. Just make sure that you understand what part of the code does the rewriting before you just blindly paste it along in there however..

    Feel free to ask any questions here.
    Last edited: Jun 19, 2012
    scylla, Jun 19, 2012 IP
  2. SpeakWhatsReal

    SpeakWhatsReal Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Actually, it would be faster if you do not use .htaccess at all and use caching methods through your server's configuration (Lighttpd, nginx / etc)
    SpeakWhatsReal, Jun 22, 2012 IP
  3. InnovusHost

    InnovusHost Peon

    Messages:
    137
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Just enabling gzip compression should be good enough to speed up your site.
    InnovusHost, Jun 23, 2012 IP
  4. scylla

    scylla Well-Known Member

    Messages:
    1,005
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    175
    #4
    Yes enabling gzip does help & SpeakWhatsReal, there are tons of people on shared hosting who don't have access to the server's config (Lighttpd, nginx / etc). For them, caching via htaccess is the way to go.
    scylla, Jun 23, 2012 IP
  5. scavy

    scavy Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This way helps better..!!

    scavy, Jun 23, 2012 IP
  6. SolidShellSecurity

    SolidShellSecurity Banned

    Messages:
    264
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    45
    #6
    We run cache and serve content from our frontend systems on our servers for all our clients. They notice faster to instant page loads almost instantly. =)
    SolidShellSecurity, Jun 23, 2012 IP
  7. atxsurf

    atxsurf Peon

    Messages:
    2,399
    Likes Received:
    21
    Best Answers:
    1
    Trophy Points:
    0
    #7
    More precisely it will allow browser & proxy caching for your static files
    atxsurf, Jun 23, 2012 IP
  8. scylla

    scylla Well-Known Member

    Messages:
    1,005
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    175
    #8
    Sure it will if you want to get all technical about it lol.
    scylla, Jun 24, 2012 IP
  9. atxsurf

    atxsurf Peon

    Messages:
    2,399
    Likes Received:
    21
    Best Answers:
    1
    Trophy Points:
    0
    #9
    seriously there is a server caching which is a whole different game
    that is if some one (or bot) hits the refresh button like crazy your mysql is not getting hit at the same rate but is being served from disk cache with no performance impact
    atxsurf, Jun 24, 2012 IP
  10. scylla

    scylla Well-Known Member

    Messages:
    1,005
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    175
    #10
    Yep that's right, glad I didn't have to explain that bit :)
    scylla, Jun 24, 2012 IP
  11. atxsurf

    atxsurf Peon

    Messages:
    2,399
    Likes Received:
    21
    Best Answers:
    1
    Trophy Points:
    0
    #11
    ps: is there an octopus stuck in your google avatar? :confused: Please free let it go!
    atxsurf, Jun 24, 2012 IP
  12. scylla

    scylla Well-Known Member

    Messages:
    1,005
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    175
    #12
    uh no, that's a google spider lol
    scylla, Jun 24, 2012 IP
  13. Brandon Sheley

    Brandon Sheley Prominent Member

    Messages:
    9,615
    Likes Received:
    589
    Best Answers:
    2
    Trophy Points:
    345
    #13
    Nice tips scylla, thanks for the updates :)

    *subscribed*
    Brandon Sheley, Jun 27, 2012 IP
  14. Harijith

    Harijith Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Thanks for sharing :) Will keep a note of it :D
    Harijith, Jul 2, 2012 IP
  15. Hostwinds_Dan

    Hostwinds_Dan Peon

    Messages:
    150
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Very useful guide - not many people know that you can immensely increase the speed of your site via the .htaccess. Thanks Scylla
    Hostwinds_Dan, Jul 3, 2012 IP
  16. Iain R

    Iain R Greenhorn

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #16
    Are these .htaccess changes still viable in 2013?
    Iain R, Mar 19, 2013 IP
  17. scylla

    scylla Well-Known Member

    Messages:
    1,005
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    175
    #17
    absolutely.
    scylla, Apr 12, 2013 IP
  18. Iain R

    Iain R Greenhorn

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #18

    Thanks. I was wondering if it were still the case
    Iain R, Apr 12, 2013 IP
  19. scylla

    scylla Well-Known Member

    Messages:
    1,005
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    175
    #19
    Alternatively, you can use google pagespeed service in conjunction with the htaccess above, as well to help with the caching. With my use of it, I believe it's best to only let it handle caching js & css, don't let it cache the images or else you will lose search rankings for them.
    scylla, Apr 12, 2013 IP
  20. palsman

    palsman Active Member

    Messages:
    156
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    95
    #20
    Thanks, it's very helpful for shared hosting
    palsman, Apr 19, 2013 IP