Magento 2.x Best Performing Magento 2 .htaccess

pickachu

New Member
Joined
Sep 10, 2019
Messages
3
Reaction score
0
Points
1
Age
28
Location
India
Speed Up Magento via Your .htaccess File

Now a word of caution before we start, this file can do some serious damage! It’s incredibly easy to knock a website over (and believe me I know) so please take care. Please take a back up of your htaccess file before making any changes, so if you do crash your website, you can simply just re-upload the original. I even take a back up of a back up I’m that paranoid.
:)


Step 1: Gzip Compression For Magento

You will need to enable Gzip Compression. To do this simple find this piece of code and uncomment it:


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

## enable resulting html compression

php_flag zlib.output_compression on

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


What I mean by uncomment it is to remove the # from the line (#php_flag zlib.output_compression on) once that is done simple save and re-upload the htaccess file.

Step Two: (optional) Compressing CSS and JavaScript files

Next we want to compress items such as CSS and javascript etc. Simply add the following lines of text directly into the htaccess file.


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

## 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

# Or, compress certain file types by extension:

<files *.html>

SetOutputFilter DEFLATE

</files>

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


For a brilliant round up of why this is important, and with some great illustrations on this step can be found at BetterExplained.com

Step Three: Leverage Browser Caching and Expires Headers

The third thing to do is Leverage Browser Caching and Expires Headers. This piece of code can make a huge difference and will reduce the number of HTTP request, which is a huge benefit for any returning visitors to your website.


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

##

<IfModule mod_expires.c>

# Enable expirations

ExpiresActive On

# Default directive

ExpiresDefault "access plus 1 month"

# My favicon

ExpiresByType image/x-icon "access plus 1 year?

# Images

ExpiresByType image/gif "access plus 1 month"

ExpiresByType image/png "access plus 1 month"

ExpiresByType image/jpg "access plus 1 month"

ExpiresByType image/jpeg "access plus 1 month"

# CSS

ExpiresByType text/css "access 1 month?

# Javascript

ExpiresByType application/javascript "access plus 1 year"

</IfModule>

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


In simpler terms when a customer visits your Magento website for the first time, their browser will then fetch all of your images, css files, javascript files, etc found on your website. In a normal situation that happens every time that visitor visits your Magento website.

With Expires headers added to you htaccess file you will be informing their browser that the files you specify are not changing until after a set time. This means that your customer’s browser won’t have to fetch everything, every time they visit your Magento website.

We have set them to expire in a month, however you can choose a longer or shorter time scale if you wish.

It might be worth looking at each individual file individually and determine how often these files could be changed, from there make a decision how long they can be cached to your customers web browser. You have 7 options:

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds
Hope you enjoy.. Works great for me...
 
|