Skip to content

Instantly share code, notes, and snippets.

@millermedeiros
Created June 11, 2012 13:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save millermedeiros/2910118 to your computer and use it in GitHub Desktop.
Save millermedeiros/2910118 to your computer and use it in GitHub Desktop.
basic htaccess for perf
DirectoryIndex index.html index.htm index.php
ErrorDocument 404 /404
# Block directory listing
Options -Indexes
# === URL Rewrite === #
# Map all URIs except those corresponding to existing files/folders to the handler
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^([\w-_\/]+)?$ ./index.php?p=$1
# === Performance === #
# Turn on expires
ExpiresActive On
# Set up gzip and strong caching and remove eTags
<Files ~ "\.(js|css)$">
SetOutputFilter DEFLATE
FileETag None
ExpiresDefault "access plus 1 year"
Header append Cache-Control "private, must-revalidate"
</Files>
# Set up strong caching and configure eTags
<Files ~ "\.(ico|gif|jpe?g|png)$">
FileETag MTime Size
ExpiresDefault "access plus 1 year"
Header append Cache-Control "public"
</Files>
# Force gzip and no-caching for dynamic files
<FilesMatch "\.(php|cgi|pl|htm?l|xml|rss|json)$">
SetOutputFilter DEFLATE
ExpiresDefault A0
Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
Header set Pragma "no-cache"
</FilesMatch>
@millermedeiros
Copy link
Author

very basic apache config file that enables some perf optimizations based on the Yahoo Performance Rules.

for more complete server settings check HTML5 boilerplate server configs

@netojoaobatista
Copy link

I really don't like to explicitly set "no-caching" for dynamic files. In fact, dynamic doesn't necessarily means "no-cacheable". So, in this case, the caching decision should be done by the application, and not in the .htaccess file.

@millermedeiros
Copy link
Author

@netojoaobatista Yes, it all depends on the kind of application you are building. For most of my projects the settings above are the expected output but sometimes JSON/XML files should also be cached forever. It is not a one size fits all solution, it should be adapted based on the needs of the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment