Apache htaccess

Last updated: 2020-05-03

Authentication

<IfModule mod_authn_file.c>
      AuthName "Username and password required"
      AuthType Basic
      AuthUserFile /vservers/absolute/path/to/.htpasswd
      <Limit GET POST>
            Require valid-user
      </Limit>
</IfModule>

Cross-site Access

bad URI or cross-site access not allowed

Add the following to the .htaccess file on the source server (where the resources are coming from, not where they are loading).

<FilesMatch "\.(ttf|otf|eot|woff|woff2)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

Users

To add a user to an existing .htpasswd file, use the htpasswd command without the -c parameter:

htpasswd .htpasswd membertwo
New password:
Re-type new password:
Adding password for user membertwo

Adjust the require line of the .htaccess file to account for the new user:

Require user originaluser membertwo

Mime Type Support

Some common MIME Types:

File TypeMIME TypeWeb Control Usage
.vtttext/vttVideo Player Web Control
.srttext/srtVideo Player Web Control
.xmltext/xmlVideo Player Web Control
.aacaudio/aacAudio Player Web Control
.ogaaudio/oggAudio Player Web Control
.mp3audio/mpegAudio Player Web Control
.mp4video/mp4Video Player Web Control
.m4vvideo/x-m4vVideo Player Web Control
.webmvideo/webmVideo Player Web Control
.ogvvideo/oggVideo Player Web Control
.flvvideo/x-flvVideo Player Web Control
.movvideo/quicktimeQuicktime Movie Web Control
.m4vvideo/x-f4vVideo Player Web Control
.m4avideo/mp4Audio Player Web Control
.svgimage/svg+xmlSVG Graphics
.jsonapplication/jsonJSON Data
.ttmlapplication/ttml+xmlVideo Player Web Control

Example configuration:

AddType image/svg+xml .svg
AddType video/ogg .ogv .ogg
AddType video/webm .webm
AddType video/x-m4v .m4v
AddType video/mp4 .mp4
AddType application/json .json

GZIP Compression

# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP

Leverage Browser Caching

## EXPIRES CACHING ##
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpg "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/pdf "access plus 1 month"
  ExpiresByType text/x-javascript "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/x-javascript "access plus 1 month"
  ExpiresByType application/x-shockwave-flash "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 year"
  ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##

Remove www

<IfModule mod_rewrite.c>
  RewriteEngine On
        RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
        RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>

Enable symbolic links

Options +FollowSymLinks

Disable index views

Options All -Indexes

Specify the default language

DefaultLanguage en

Specify the default character set

AddDefaultCharset utf-8

BEGIN GZIP

<ifmodule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP