Skip to content

Instantly share code, notes, and snippets.

@exiguus
Created April 23, 2019 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exiguus/42f329e095d809cd398ca65836268122 to your computer and use it in GitHub Desktop.
Save exiguus/42f329e095d809cd398ca65836268122 to your computer and use it in GitHub Desktop.
Light House Recommendations for Apache2
# .htaccess
# Apache/2.4.10 (Debian)
#
# AllowOverride FileInfo Indexes Options
##############################################
# Caching
##############################################
<IfModule headers_module>
# YEAR 31536000
# MONTH 2592000
# WEEK 604800
# DAY 43200
# YEAR
<FilesMatch "\.(svg|gif|jpg|jpeg|png|webm|webp|ico|flv|swf|mp?g|avi|divx|mp4|mp3|ogg|wma|wmv|mov|m4|eot|ttf|woff|woff2|otf)$">
Header set Cache-Control "max-age=31536000"
</FilesMatch>
# MONTH
<FilesMatch "\.(vcf|js|css|pdf|esp|psd|txt|json|ascii|log|doc?|xls|od?|ppt|zip|tar|tgz|tbz|gz|bz|bz2|bzip|rar|7z|sh|bash|phps|exe|cmd|bat)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
# WEEK
Header set Cache-Control "max-age=604800"
# DONT
<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
<FilesMatch "\.(js|css|json|xml|gz|html)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
##############################################
# Start Redirect Rules
# AllowOverride:
##############################################
# 301 is permanent (directed from page will be removed from search engines)
# 302 is temporary (directed from page will be indexed by search engines in the future)
#Redirect 301 http://example.org/software http://example.org/
#Redirect 301 /software/$ http://example.org
# match trailing slash
#RedirectMatch 301 /public/? http://example.org/software/public
# direct trailing slash and match
#Redirect 301 /software/public/bsd http://example.org/software/public/unix/
#RedirectMatch 301 /software/public/bsd/ http://example.org/software/public/unix/
#Redirect 301 /test.html http://foo.de
##############################################
# Start Rewrite Rules
# AllowOverride:
# RewriteEngine, RewriteOptions,
# RewriteBase, RewriteCond, RewriteRule
##############################################
# 301 is permanent (directed from page will be removed from search engines)
# 302 is temporary (directed from page will be indexed by search engines in the future)
#<IfModule rewrite_module> server should fail if a2mod is not enabled
RewriteEngine On
##############################################
# http://domain.tld/$1 to https://domain.tld/$1
# likly pound or something do this
#RewriteCond %{HTTPS} !=on
#RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [L,R=301]
##############################################
# domain.tld to www.domain.tld
# likly pound or something do this
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
##############################################
# Language index
#RewriteCond %{HTTP:Accept-Language} ^de.*$ [NC]
#RewriteCond %{REQUEST_URI} ^/en/error.html$ [NC]
#RewriteRule ^(.*)$ /de/fehler.html [L,R=302]
#RewriteCond %{HTTP:Accept-Language} ^de.*$ [NC]
#RewriteCond %{REQUEST_URI} ^(/|/index.html|!error.html)$ [NC]
#RewriteRule ^(.*)$ /de/ [L,R=302]
#RewriteCond %{HTTP:Accept-Language} !^de.*$ [NC]
#RewriteCond %{REQUEST_URI} ^(/|/index.html|!error.html)$ [NC]
#RewriteRule ^(.*)$ /en/ [L,R=302]
##############################################
# Other
#RewriteRule ^level1/(.*)$ http://domain.tld/level1/level2/$1 [L,R=301]
# END ModRewrite
#</IfModule>
##############################################
# Language and Charsets
# AllowOverride:
##############################################
AddDefaultCharset UTF-8
# AddCharset UTF-8 .txt .ascii .php .shtml .html .htm .css .js .htc
AddCharset UTF-8 .txt .ascii .html .htm .css .js .json .htc .xml
#DefaultLanguage de-DE
#AddLanguage de-DE .txt .ascii .php .shtml .html .htm .css .js .htc
# webp images mimetype support
AddType image/webp .webp .WEBP
##############################################
# Directory Options
# AllowOverride:
# DirectoryIndex, Options
##############################################
# Options +SymLinksIfOwnerMatch -ExecCGI -Indexes -Includes
Options -ExecCGI -Indexes -Includes
DirectoryIndex index.html
##############################################
# Compress / deflate
# AllowOverride:
# SetOutputFilter
# SetEnvIf, SetEnvIfNoCase
# BrowserMatch
# Header
##############################################
# Insert filter
SetOutputFilter DEFLATE
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don't compress because they are
SetEnvIfNoCase Request_URI \.(?:gif|jp?g|png|ico|flv|swf|mp?g|avi|divx|mp?|ogg|wm?|mov|m4)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|zip|tar|tgz|tbz|gz|bz|bz2|bzip|rar|7z)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
##############################################
FileETag INode MTime Size
##############################################
# Error Documents
# AllowOverride:
# ErrorDocument
##############################################
# ErrorDocument NNN /path/to/file or "Message
# Client Request Errors
ErrorDocument 400 "Error 400 Bad Request
ErrorDocument 401 "Error 401 Unauthorized
ErrorDocument 402 "Error 402 Payment Required
ErrorDocument 403 /error/
ErrorDocument 404 /error/
ErrorDocument 405 "Error 405 Method Not Allowed
ErrorDocument 406 "Error 406 Not Acceptable
ErrorDocument 407 "Error 407 Proxy Authentication Required
ErrorDocument 408 "Error 408 Request Time-out
ErrorDocument 409 "Error 409 Conflict
ErrorDocument 410 "Error 410 Gone
ErrorDocument 411 "Error 411 Length Required
ErrorDocument 412 "Error 412 Precondition Failed
ErrorDocument 413 "Error 413 Request Entity Too Large
ErrorDocument 414 "Error 414 Request-URI Too Long
ErrorDocument 415 "Error 415 Unsupported Media Type
ErrorDocument 416 "Error 416 Requested range not satisfiable
ErrorDocument 417 "Error 417 Expectation Failed
ErrorDocument 422 "Error 422 Unprocessable Entity
ErrorDocument 423 "Error 423 Locked
ErrorDocument 424 "Error 424 Failed Dependency
ErrorDocument 426 "Error 426 Upgrade Required
#Server Errors
ErrorDocument 500 "Error 500 Internal Server Error - Service Temporary Unavailable
ErrorDocument 501 "Error 501 Not Implemented
ErrorDocument 502 "Error 502 Bad Gateway
ErrorDocument 503 "Error 503 Service Unavailable
ErrorDocument 504 "Error 504 Gateway Time-out
ErrorDocument 505 "Error 505 HTTP Version not supported
ErrorDocument 506 "Error 506 Variant Also Negotiates[6]
ErrorDocument 507 "Error 507 Insufficient Storage
ErrorDocument 510 "Error 510 Not Extended
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment