Magento htaccess file: How to enable developer mode and optimize it

What is Magento htaccess file?

The file is especially critical for the Search Engine Friendly URLs as it’s applied for keeping mod-rewrite rules. It also includes a standard web server and php directives, which can be modified for improvement of a website performance. You will need the backup of the htaccess file Magento htaccess file sample (.htaccess.sample) when it is required to extract appropriate code lines or to substitute it instead of the current when it won’t work after an update.   

How to enable Magento developer mode htaccess?

First off, you need to check everything is prepared:

  • Disable Cache System > Cache Management > Select All [Check-Boxes] > Actions = Disable > Submit
  • Re-Index All System > Index Management > Select All [Check-Boxes] > Actions = Reindex Data > Submit
  • Disable Complication System > Tools > Compilation > Disable
  • Turn On Logging System > Configuration > Advanced > Developer > Log Settings > Enabled => Yes 

The mode can be enabled either manually (M1/2) or from the command line (only in M2):

E.g.: for Magento 1: MAGE_IS_DEVELOPER_MODE should be set to any value:

Listen 807
<VirtualHost 127.0.0.1:807>
          ServerName example.com
          DocumentRoot ".../magento/.../code"
          SetEnv MAGE_IS_DEVELOPER_MODE "any value"
</VirtualHost> 
 

E.g.: for Magento 2: MAGE_MODE should be set to developer value:

Listen 1900
<VirtualHost 127.0.0.1:1900>
          ServerName example.com
          DocumentRoot ".../magento2"
          SetEnv MAGE_MODE "developer"
          SetEnv MAGE_RUN_TYPE "website"
          SetEnv MAGE_RUN_CODE "mage2"
          RemoteIPHeader X-Forwarded-For

</VirtualHost>

E.g.: command line:

> bin/magento deploy:mode:set developer
############################################
## overrides deployment configuration mode value
## use command bin/magento deploy:mode:set to switch modes
 
#   SetEnv MAGE_MODE developer
 
############################################
## uncomment these lines for CGI mode
## make sure to specify the correct cgi php binary file name
## it might be /cgi-bin/php-cgi
 
#    Action php5-cgi /cgi-bin/php5-cgi

#    AddHandler php5-cgi .php

Magento htaccess Optimization

If you want to optimize the file, choose the optimizing htaccess files and past it into the configuration for the virtual host;

Here are the pieces of essential code to optimize Magento .htaccess file:

 SetEnvIf Host www\.example1\.com MAGE_RUN_CODE=example1
SetEnvIf Host www\.example1\.com MAGE_RUN_TYPE=store
SetEnvIf Host www\.example2\.com MAGE_RUN_CODE=example2
SetEnvIf Host www\.example2\.com MAGE_RUN_TYPE=store
##### Block unwanted Bots that clog the server #####
RewriteCond %{HTTP_USER_AGENT} MJ12bot
RewriteRule .* - [F]
RewriteCond %{HTTP_USER_AGENT} 80legs [NC]
RewriteRule ^ - [F]
##### Add support for SVG Graphics and CSS3 Pie #####
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
AddType text/x-component .htc
DirectoryIndex index.php
##### Domain-specific PHP Settings #####
php_value memory_limit 1024M
php_value max_execution_time 18000
php_flag magic_quotes_gpc off
php_flag session.auto_start off
php_flag suhosin.session.cryptua off
php_flag zend.ze1_compatibility_mode Off
##### Necessary redirects and rewrites for search engines #####
RewriteCond %{HTTP_HOST} !^www.example1.com$ [NC]
RewriteRule ^(.*)$ http://www.example1.com/$1 [R=301,L]
##### Redirect away from /index.php and /home
##### Warning: This index.php rewrite will prevent Magento
##### Connect from working. Simply comment out the
##### following two lines of code when using Connect.
##### Please note - http://www. if not using www simply use http://
##### Please note - http://www. if not using www simply use http://
redirect 301 /home http://www.example.com
Options +FollowSymLinks
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
##### mod_deflate compresses your output to lower the file size being sent to the client #####
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
php_flag zlib.output_compression on
SetOutputFilter DEFLATE
SSLOptions StdEnvVars
##### disable POST processing to not break multiple image upload #####
SecFilterEngine Off
SecFilterScanPOST Off
##### Enable apache served files compression #####
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
AddOutputFilterByType DEFLATE image/x-icon
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
##### Mod gzip and caching for improved site speed #####
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
##### Default expires headers for all file types #####
##### Not recommended for development environment #####
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/html "access plus 1 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year”
##### Mime Type Caching #####
##### AUDIO #####
AddType audio/mp4 m4a f4a f4b
AddType audio/ogg oga ogg
##### JAVASCRIPT #####
# Normalize to standard type (it's sniffed in IE anyways):
# http://tools.ietf.org/html/rfc4329#section-7.2
AddType application/javascript js jsonp
AddType application/json json
##### VIDEO #####
AddType video/mp4 mp4 m4v f4v f4p
AddType video/ogg ogv
AddType video/webm webm
AddType video/x-flv flv
##### WEB FONTS #####
AddType application/font-woff woff
AddType application/vnd.ms-fontobject eot
##### Browsers usually ignore the font MIME types #####
##### and sniff the content, however, Chrome shows #####
##### a warning if other MIME types are used for #####
##### the following fonts. #####
AddType application/x-font-ttf ttc ttf
AddType font/opentype otf
##### OTHER #####
AddType application/octet-stream safariextz
AddType application/x-chrome-extension crx
AddType application/x-opera-extension oex
AddType application/x-shockwave-flash swf
AddType application/x-web-app-manifest+json webapp
AddType application/x-xpinstall xpi
AddType application/xml atom rdf rss xml
AddType image/webp webp
AddType image/x-icon ico
AddType text/cache-manifest appcache manifest
AddType text/vtt vtt
AddType text/x-component htc
AddType text/x-vcard vcf
AddCharset utf-8 .atom .css .js .json .rss .vtt .webapp .xml
##### Disable ETags http://developer.yahoo.com/performance/rules.html#etags #####
FileETag None
##### Prevent character encoding issues from server overrides #####
AddDefaultCharset Off
#AddDefaultCharset UTF-8
##### Force IE8 compatibility when using IE8+ #####
##### May cause issues within Windows Mobile Browsers #####
BrowserMatch MSIE best-standards-support
Header set X-UA-Compatible IE=8 env=best-standards-support
##### By default allow all access #####
Order allow,deny
Allow from all
 

Magento htaccess 301 redirect: how to avoid 404 error?

When redirecting an individual file like example.com/oldfile.htm run a 301 redirect as the following:

Redirect 301 /oldfile.htm /newfile.htm

But when working at redirects the internal system usage is more advisable. Also, you can look through our error guide in the blog, which describes other glitches like ‘one URL operates well only’. Besides, you can take advantage of Errors Log extension.

Rate the answer?