Htaccess Structure

This is our proposed .htaccess file structure order:

  1. Litespeed stanza
  2. Hidden files and folders protection (with auto SSL exception)
  3. HTTP to HTTPS redirection
  4. WordPress stanza
  5. Adaptive Images (where necessary)
  6. etc

Sample .htaccess

################################################################################
#                                                                              #
#   LITESPEED STUFF                                                            #
#                                                                              #
################################################################################
#                                                                              #

# BEGIN LSCACHE
## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
<IfModule LiteSpeed>
RewriteEngine on
CacheLookup on
RewriteRule .* - [E=Cache-Control:no-autoflush]
RewriteRule ^min/\w+\.(css|js) - [E=cache-control:no-vary]

### marker FAVICON start ###
RewriteRule favicon\.ico$ - [E=cache-control:max-age=86400]
### marker FAVICON end ###

</IfModule>
## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
# END LSCACHE
# BEGIN NON_LSCACHE
## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
### marker MINIFY start ###
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} ^(.*)/min/(\w+)\.(css|js)$
RewriteCond %1/wp-content/cache/$2/$1.$2 -f
RewriteRule min/(\w+)\.(css|js) wp-content/cache/$2/$1.$2 [L]
</IfModule>
### marker MINIFY end ###

## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##
# END NON_LSCACHE

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#   PREVENT ACCESS TO HIDDEN FILES                                             #
#                                                                              #
################################################################################
#                                                                              #

    # Prevent access to hidden files (but allow for AutoSSL)
    #=======================================================

    <IfModule mod_rewrite.c>
        RewriteCond %{SCRIPT_FILENAME} -d [OR]
        RewriteCond %{SCRIPT_FILENAME} -f
        RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
        RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
        RewriteRule "(^|/)\." - [F]
    </IfModule>

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#   HTTPS/SSL REDIRECTION                                                      #
#                                                                              #
################################################################################
#                                                                              #

    # Redirect all traffic to https
    #==============================

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#   WORDPRESS CONFIG (HANDLED AUTOMATICALLY BY WORDPRESS)                      #
#                                                                              #
################################################################################
#                                                                              #

# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#   ADAPTIVE IMAGES                                                            #
#                                                                              #
################################################################################
#                                                                              #

# BEGIN Adaptive Images
#=======================

<IfModule mod_rewrite.c>

    RewriteEngine On

    # Watched directories
    RewriteCond %{REQUEST_URI} /wp-content/uploads [OR]
    RewriteCond %{REQUEST_URI} /wp-content/themes

    # Redirect images through the adaptive images script
    RewriteRule \.(?:jpe?g|gif|png)$ /wp-content/plugins/adaptive-images/adaptive-images-script.php [L]

</IfModule>

# END Adaptive Images

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#   GENERAL CONFIGURATIONS                                                     #
#                                                                              #
################################################################################
#                                                                              #

    # Disallow directory listing
    #============================

    Options -Indexes

    # Make sure UTF-8 encoding is default
    #=====================================

    AddDefaultCharset utf-8

    # Disallow embedding as an iframe from other domains
    #====================================================
    
    Header set X-Frame-Options "SAMEORIGIN"

    <FilesMatch "\.(appcache|atom|bmp|css|cur|eot|flv|geojson|gif|htc|ico|jpe?g|js|json(ld)?|m4[av]|mp4|og[agv]|opus|otf|pdf|png|rdf|rss|svgz?|swf|tt[cf]|txt|web[mp]|woff2?|xml)$">
        Header unset X-Frame-Options
    </FilesMatch>

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#   DEFINE EXTRA MIME TYPES                                                    #
#                                                                              #
################################################################################
#                                                                              #

    # Fonts
    #=======

    AddType application/font-woff2        woff2
    AddType application/font-woff         woff
    AddType application/vnd.ms-fontobject eot
    AddType application/x-font-ttf        ttc ttf
    AddType font/opentype                 otf

    # Favicons
    #==========
    
    AddType image/x-icon                  cur ico
    AddType image/vnd.microsoft.icon      ico

    # Media files
    #=============
    
    AddType image/svg+xml                 svg svgz
    AddType image/bmp                     bmp
    AddType image/webp                    webp
    AddType audio/mp4                     f4a f4b m4a
    AddType audio/ogg                     oga ogg opus
    AddType video/mp4                     f4v f4p m4v mp4
    AddType video/ogg                     ogv
    AddType video/webm                    webm
    AddType video/x-flv                   flv
    
    # Application files
    #===================

    AddType application/json              json map topojson
    AddType application/javascript        js
    AddType application/xml               atom rdf rss xml

    # Misc
    #======

    AddType text/x-component              htc

    AddEncoding gzip svgz

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#  BROWSER CACHING EXPIRES                                                     #
#                                                                              #
################################################################################
#                                                                              #

    # Activate caching headers
    #==========================
    
    ExpiresActive On

    # Set caching for different mime types
    #======================================

    ExpiresDefault                              "access plus 1 week"

    ExpiresByType text/html                     "access plus 1 day"

    ExpiresByType text/css                      "access plus 1 month"
    ExpiresByType application/javascript        "access plus 1 month"
    ExpiresByType application/x-javascript      "access plus 1 month"
    ExpiresByType text/javascript               "access plus 1 month"

    ExpiresByType text/xml                      "access plus 1 day"
    ExpiresByType application/xml               "access plus 1 day"
    ExpiresByType application/rss+xml           "access plus 1 day"
    ExpiresByType application/atom+xml          "access plus 1 day"
    ExpiresByType application/rdf+xml           "access plus 1 day"

    ExpiresByType image/jpg                     "access plus 3 months"
    ExpiresByType image/jpeg                    "access plus 3 months"
    ExpiresByType image/png                     "access plus 3 months"
    ExpiresByType image/gif                     "access plus 3 months"
    ExpiresByType image/bmp                     "access plus 3 months"
    ExpiresByType image/svg+xml                 "access plus 3 months"

    ExpiresByType audio/ogg                     "access plus 3 months"
    ExpiresByType video/mp4                     "access plus 3 months"
    ExpiresByType video/ogg                     "access plus 3 months"
    ExpiresByType video/webm                    "access plus 3 months"

    ExpiresByType image/vnd.microsoft.icon      "access plus 6 months"
    ExpiresByType image/x-icon                  "access plus 6 months"

    ExpiresByType x-font/font                   "access plus 6 months"
    ExpiresByType font/eot                      "access plus 6 months"
    ExpiresByType font/opentype                 "access plus 6 months"
    ExpiresByType application/x-font-ttf        "access plus 6 months"
    ExpiresByType application/font-woff         "access plus 6 months"
    ExpiresByType application/x-font-woff       "access plus 6 months"
    ExpiresByType font/woff                     "access plus 6 months"
    ExpiresByType application/font-woff2        "access plus 6 months"
    ExpiresByType application/vnd.ms-fontobject "access plus 6 months"

    # Turn ETag headers off
    #=======================

    Header unset ETag

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#   ADD CONTENT COMPRESSION                                                    #
#                                                                              #
################################################################################
#                                                                              #

    # For text, html, javascript, css, xml and fonts
    #================================================

    AddOutputFilterByType DEFLATE "text/html" \
                                  "text/plain" \
                                  "text/css" \
                                  "text/xml" \
                                  "text/x-component" \
                                  "application/xml" \
                                  "application/xhtml+xml" \
                                  "application/atom+xml" \
                                  "application/rdf+xml" \
                                  "application/rss+xml" \
                                  "text/javascript" \
                                  "application/json" \
                                  "application/javascript" \
                                  "application/x-javascript" \
                                  "x-font/font" \
                                  "application/vnd.ms-fontobject" \
                                  "application/x-font-ttf" \
                                  "application/x-web-app-manifest+json" \
                                  "font/eot" \
                                  "font/opentype" \
                                  "image/bmp" \
                                  "image/svg+xml" \
                                  "image/vnd.microsoft.icon" \
                                  "image/x-icon"

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#   DISSALLOW ACCESS TO SENSITIVE FILES                                        #
#                                                                              #
################################################################################
#                                                                              #

    # Disallow access to WordPress related files
    #===========================================

    <Files wp-config.php>
        order deny,allow
        Deny from all
    </Files>
    <Files wp-login.php>
        order deny,allow
        Deny from all
    </Files>
    <Files wp-register.php>
        order deny,allow
        Deny from all
    </Files>
    <Files readme.html>
        order deny,allow
        Deny from all
    </Files>
    <Files error_log>
        order deny,allow
        Deny from all
    </Files>
    <Files debug.log>
        order deny,allow
        Deny from all
    </Files>

    # Disallow direct access to core include files
    #==============================================

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^wp-admin/includes/ - [F,L]
        RewriteRule !^wp-includes/ - [S=3]
        RewriteRule ^wp-includes/[^/]+.php$ - [F,L]
        RewriteRule ^wp-includes/js/tinymce/langs/.+.php - [F,L]
        RewriteRule ^wp-includes/theme-compat/ - [F,L]
    </IfModule>

    # Disallow direct access to PHP files in uploads
    #================================================

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule wp-content/uploads/(.*\.php)$ - [R=403,L]
    </IfModule>

    # Protect Gravity Forms uploads in general from direct access
    #=============================================================

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule wp-content/uploads/gravity_forms/.*$ - [R=403,L]
    </IfModule>

    # Prevent user enumeration urls
    #==============================
    
    <IfModule mod_rewrite.c>
        RewriteCond %{QUERY_STRING} author=d
        RewriteRule ^ /? [L,R=301]
    </IfModule>

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#   DISABLE XMLRPC                                                             #
#                                                                              #
################################################################################
#                                                                              #

    <Files xmlrpc.php>
        order deny,allow
        Deny from all
        # Exceptions
        # allow from xxx.xxx.xxx.X/XX
    </Files>

#                                                                              #
################################################################################

################################################################################
#                                                                              #
#   CREDITS AND KUDOs TO THESE GUYS:                                           #
#                                                                              #
################################################################################
#                                                                              #

    # 1. https://github.com/Creare/WP-htaccess/
    # 2. https://github.com/h5bp/server-configs-apache/
    # 3. http://brechtpalombo.com/stop-wordpress-xmlrpc-php-attack-still-use-jetpack/
    # 4. http://forum.ait-pro.com/forums/topic/wordpress-xml-rpc-ddos-protection-protect-xmlrpc-php-block-xmlrpc-php-forbid-xmlrpc-php/

#                                                                              #
################################################################################