best magento htaccess for performance
-
16-10-2019 - |
Question
we have installed several extensions and bits and pieces of code to optimize Magento htaccess. Now I see the code has gotte quite long and I am not sure this is all conflicting or supporting ...
So this is what we have so far. What are the best settings?
############################################
## http://www.magentocommerce.com/wiki/groups/227/allowed_memory_size_exhausted
## Increase memory limit according to Magento
php_value memory_limit 64M
php_value max_execution_time 18000
############################################
## enable resulting html compression of php output
php_flag zlib.output_compression on
############################################
## enable apache served files compression (not php, html/images/css/js
## http://developer.yahoo.com/performance/rules.html#gzip
<IfModule mod_deflate.c>
# Insert filter on all content
SetOutputFilter DEFLATE
# Insert filter on selected content types only
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
# 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 images
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
############################################
## Enable keep-alives to reduce # http requests
## http://yoast.com/magento-performance-hosting/
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
############################################
## Add default Expires header
## http://developer.yahoo.com/performance/rules.html#expires
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 14 days"
</IfModule>
and as I understand the next improvement is moving all these rules in htaccess to the virtualhost configuration directives mentioned here:
Solution
You could increase the memory_limit
to the minimum recommended 256MB. Outside of that, there's no real performance improvement to be gained in editing your htaccess.
The best htaccess is not having one at all. Move and/or rewrite your rules into your vhost definition in Apache and disable overrides.
For highest performance use AllowOverride None everywhere in your filesystem.
Source: http://httpd.apache.org/docs/current/misc/perf-tuning.html#htaccess
OTHER TIPS
By default, Magento comes with a .htaccess file that you can use for your installation. However, this .htaccess file is not optimized and misses a lot of things that can help your site. Therefor, we have created a .htaccess file for Magento that focusses on site speed, security and SEO. You can copy and paste it to use it as your own .htaccess file on your server.
#########################################################################
################## Coding Basics Magento .htaccess File #################
# More Info: http://codingbasics.net/magento-htaccess-file-optimized-speed-security-seo/ #
#########################################################################
##### Block unwanted Crawler Bots that clog your server #####
RewriteCond %{HTTP_USER_AGENT} MJ12bot
RewriteRule .* - [F]
RewriteCond %{HTTP_USER_AGENT} 80legs [NC]
RewriteRule ^ - [F]
##### Add support for SVG Images and CSS3 Pie #####
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
AddType text/x-component .htc
DirectoryIndex index.php
##### PHP Settings for your domain #####
<IfModule mod_php5.c>
php_value memory_limit 512M
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
</IfModule>
##### Search Engine redirects and rewrites for SEO purposes #####
<IfModule mod_rewrite.c>
#RewriteCond %{HTTP_HOST} !^www.yourdomain.com$ [NC]
#RewriteRule ^(.*)$ http://www.yourdomain.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://
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.yourdomain.com/$1 [R=301,L]
##### Please note - http://www. if not using www simply use http://
redirect 301 /home http://www.yourdomain.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]
</IfModule>
##### mod_deflate compresses your output to lower the file size being sent to the client #####
<IfModule mod_deflate.c>
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
</IfModule>
<Files *.php>
SetOutputFilter DEFLATE
</Files>
<IfModule mod_ssl.c>
SSLOptions StdEnvVars
</IfModule>
##### Header Directives #####
<ifModule mod_headers.c>
Header unset ETag
Header unset Last-Modified
</ifModule>
##### disable POST processing to prevent breaking image upload #####
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
##### Served files compression on Apache for improved site speed #####
<IfModule mod_deflate.c>
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
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
</IfModule>
##### Improve site speed through Mod gzip and caching #####
<ifModule mod_gzip.c>
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.*
</ifModule>
##### Default expires headers for all file types #####
##### Not recommended for development environment #####
<ifModule mod_expires.c>
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"
</ifModule>
##### Mime Type Caching Magento #####
<IfModule mod_mime.c>
##### 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
</IfModule>
<IfModule mod_mime.c>
AddCharset utf-8 .atom .css .js .json .rss .vtt .webapp .xml
</IfModule>
##### 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
The best .htaccess is no .htaccess. That means migrating all the .htaccess rules into your Apache (or other flavour of Web server) configuration.
One really good way to do this is to search for all .htaccess files in the Magento root and subfolders, combine these into one correctly formatted file and include this in your Apache config :
# Include combined Magento .htaccess files
<Directory /your_magento_root_here>
AllowOverride None
</Directory>
Include /your_magento_root_here/.htaccess-combined
More information as to how to achieve this and a script you can use to create the combined .htaccess can be found here:
http://colin.mollenhour.com/2010/06/30/the-right-way-to-optimize-apaches-htaccess-files/
Whenever you upgrade Magento, run the script again and restart your web server to pick up any changes in .htaccess rules.
Here is my blog in .htaccess file in Magento.
http://blog.rahuldadhich.com/magento-speed-optimization-htaccess/
Hope this will help.