Skip to content

Instantly share code, notes, and snippets.

@caycefischer
Created February 18, 2013 09:40
Show Gist options
  • Save caycefischer/4976236 to your computer and use it in GitHub Desktop.
Save caycefischer/4976236 to your computer and use it in GitHub Desktop.
.htaccess shenanigans
# redirect www to no-www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
# custom error pages
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
# set cache-control headers
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
# explicitly disable caching for scripts and other dynamic files
<FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
# prevent hotlinking
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?domain.com/.*$ [NC]
RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]
# secure htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
# prevent viewing of a specific file
<Files secretfile.jpg>
order allow,deny
deny from all
</Files>
# the above, for multiple file types
<FilesMatch ".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
Order Allow,Deny
Deny from all
</FilesMatch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment