Search code examples
.htaccess

Need help cleaning up .htaccess


I am looking at an .htaccess file in a project that I'm helping with and it appears that some of the code in it is redundant. Can it be cleaned-up a bit? Here's the code:

<Files .htaccess>
order allow,deny
deny from all
</Files>

# Add Caching
<FilesMatch "\.(png|gif|js|css)$">
  ExpiresActive on
  ExpiresDefault "access plus 1 month"
</FilesMatch>

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


# disable directory autoindexing
Options -Indexes

ErrorDocument 400 https://domain.name
ErrorDocument 401 https://domain.name
ErrorDocument 402 https://domain.name
ErrorDocument 403 https://domain.name
ErrorDocument 405 https://domain.name
ErrorDocument 404 /incl/pages/error404.php
ErrorDocument 500 https://domain.name

RewriteEngine On

# https
RewriteCond %{HTTPS} off
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [L,R=301]

# Redirect to domain without www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]

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

# Stop hotlinking.
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} ^https?://([^/]+)/ [NC]
RewriteCond %1#%{HTTP_HOST} !^(.+)#\1$
RewriteRule \.(jpg|jpeg|png|gif|swf|svg)$ - [NC,F,L]

# Compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

Solution

  • You have couple of extra rewrite rules that can be merged into a single redirect for https and www removal.

    Here is the suggested full .htaccess:

    <Files .htaccess>
    order allow,deny
    deny from all
    </Files>
    
    # Add Caching
    <FilesMatch "\.(png|gif|js|css)$">
      ExpiresActive on
      ExpiresDefault "access plus 1 month"
    </FilesMatch>
    
    # EXPIRES CACHING ##
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType image/jpg "access 1 year"
        ExpiresByType image/jpeg "access 1 year"
        ExpiresByType image/gif "access 1 year"
        ExpiresByType image/png "access 1 year"
        ExpiresByType text/css "access 1 month"
        ExpiresByType text/html "access 1 month"
        ExpiresByType application/pdf "access 1 month"
        ExpiresByType text/x-javascript "access 1 month"
        ExpiresByType image/x-icon "access 1 year"
        ExpiresByType application/x-httpd-php "access plus 1 month"
        ExpiresDefault "access 1 month"
    </IfModule>
    # EXPIRES CACHING ##
    
    # disable directory autoindexing
    Options -Indexes
    
    ErrorDocument 400 https://domain.name
    ErrorDocument 401 https://domain.name
    ErrorDocument 402 https://domain.name
    ErrorDocument 403 https://domain.name
    ErrorDocument 405 https://domain.name
    ErrorDocument 404 /incl/pages/error404.php
    ErrorDocument 500 https://domain.name
    
    RewriteEngine On
    
    # remove www and turn on https in the same rule
    RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
    RewriteCond %{HTTPS} !on
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
    
    # Stop hotlinking.
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} ^https?://([^/]+)/ [NC]
    RewriteCond %1#%{HTTP_HOST} !^(.+)#\1$
    RewriteRule \.(jpe?g|png|gif|swf|svg)$ - [NC,F,L]
    
    # BEGIN GZIP
    <ifmodule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript application/x-httpd-php
    </ifmodule>
    # END GZIP