Skip to content

Instantly share code, notes, and snippets.

@TimFletcher
Forked from jolantis/.htaccess
Created August 14, 2010 22:56
Show Gist options
  • Save TimFletcher/524807 to your computer and use it in GitHub Desktop.
Save TimFletcher/524807 to your computer and use it in GitHub Desktop.
<IfModule mod_rewrite.c>
RewriteEngine On
# Don't allow users to view robots.txt file
# RewriteCond %{REQUEST_URI} !^/robots\.txt$
# Force add 'www' to URIs without 'www' (permanent redirect)
# RewriteCond %{HTTP_HOST} ^yourdomainname\.com$ [NC]
# RewriteRule ^(.*)$ http://www.yourdomainname.com/$1 [L,R=301]
# Force remove 'www' from URIs with 'www' (permanent redirect)
# RewriteCond %{HTTP_HOST} ^www.yourdomainname.com$ [NC]
# RewriteRule ^(.*)$ http://yourdomainname.com/$1 [R=301,L]
# Forse URIs *without* trailing slashes
# RewriteCond %{REQUEST_METHOD} !=POST
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.+)/$ /$1 [R=301,L]
# Remove/hide index.php form EE URIs
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ /index.php?/$1 [L]
# Rewrite rules for Smartoptimizer [1/3] (http://code.google.com/p/smartoptimizer/)
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*\.(js|css))$ smartoptimizer/?$1 [L]
<IfModule mod_expires.c>
# More rewrite rules for Smartoptimizer [2/3]
# RewriteCond %{REQUEST_FILENAME} -f
# RewriteRule ^(.*\.(js|css|html?|xml|txt))$ smartoptimizer/?$1
# Set expires headers
<FilesMatch "\.(gif|jpg|jpeg|png|swf|flv|pdf|ico|avi|mov|ppt|doc|mp3|wmv|wav|txt|css|js|html?|php|xml|eot|ttf|otf|woff)$">
ExpiresActive On
ExpiresDefault "access plus 1 year"
FileETag Size
</FilesMatch>
# Set Cache-Control headers
<FilesMatch "\.(gif|jpg|jpeg|png|swf|flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|txt|eot|ttf|otf|woff)$">
Header append Cache-Control: public
</FilesMatch>
<FilesMatch "\.(css|js|html?|php|xml)$">
Header append Cache-Control: private
</FilesMatch>
</IfModule>
<IfModule !mod_expires.c>
# More rewrite rules for Smartoptimizer [3/3]
# RewriteCond %{REQUEST_FILENAME} -f
# RewriteRule ^(.*\.(gif|jpg|jpeg|png|ico|swf|css|js|html?|xml|txt))$ smartoptimizer/?$1
</IfModule>
</IfModule>
<IfModule mod_headers.c>
<IfModule mod_mime.c>
# Add custom mime type, to be able to use it in the 'AddOutputFilterByType' declaration (gzip font-face files: http://www.phpied.com/gzip-your-font-face-files/)
AddType application/vnd.ms-fontobject .eot
AddType font/truetype .ttf
AddType font/opentype .otf
AddType font/woff .woff
# Proper svg serving. Required for svg webfonts on iPad (http://twitter.com/FontSquirrel/status/14855840545)
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
</IfModule>
# Apache 1.3 uses mod_gzip while Apache 2.x uses mod_deflate (http://httpd.apache.org/docs/2.0/mod/mod_deflate.html)
<IfModule mod_deflate.c>
# Enable the compression (gzip) for any type of content
# SetOutputFilter DEFLATE
# Or (alternatively) define which filetypes should pass through the DEFLATE output filter (gzip compression)
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript application/json
# html, xml, css, and js above, webfonts and svg below
<FilesMatch "\.(ttf|otf|woff|eot|svg|svgz)$" >
SetOutputFilter DEFLATE
</FilesMatch>
# Turne-off (or restrict) compression to files of type text/html for known problematic web browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# File types not to be compressed
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:swf|flv|pdf)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:avi|mov)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:mp3|mp4)$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
# Allow access from all domains for webfonts (alternatively you could only whitelist your subdomains like "sub.domain.com")
<FilesMatch "\.(ttf|otf|eot|woff|font.css)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment