Speed Up Magento Via Your Htaccess File | Make Magento Fast, Really Fast!

At Aduro Labs we have a number of clients who run large websites on Magento. Magento is an advanced ecommerce platform, and has an awful lot of functions to make selling online easy. Even though it can take some time to make relatively simple changes, it does exactly what it says on the tin.

One issue we have found with Magento is its loading speed. Load times are becoming more and more important online with some of the major search engines recommending that websites load as quickly as possible. Google for instance has even produced a tool to help measure your overall page loading time and even tells you exactly what needs to be corrected to improve that time.

Before we go any further let’s listen to what the head of Google’s web spam team; Matt Cutts, has to say about web speed. The video is a bit dated now (2011) however the topics discussed are still valid:

So, where do you start with speeding up Magento? The first thing you need to do is to analyse the overall speed of your Magento page, before any changes have been made. We would recommend three tools which can be found below:

  • Google PageSpeed Insights
  • WebPageTest
  • GTmetrix

We would always recommend using two or three tools to analyse website page speed, as some will give more detail than others and speed scores to vary from tool to tool.

We ran the website through the Google PageSpeed Insights first to get an initial overview of the website page speed.

Speed Up Magento

As you can see the overall page speed was not a brilliant 38 out of 100, so we knew we had a little bit of work to do.

Getmetrix scores your websites page speed in A-F grades and as you can see below the initial results were not great:

Magento Speed Test

With theses speed tools telling us exactly what needed to be altered we set to work.

Speed Up Magento Via The Admin Panel

One of the issues that the Google PageSpeed Insights tool suggested as a high priority was to merge CCS and Java script files of the website. Now, if you are sat there thinking how on earth do I do that, have no fear!

Magento has a few features accessible within its admin panel to allow you to merge CCS and Java script files of the website, which can be found by clicking on the following menu options:

System > Configuration > Developer > JavaScript Settings

Once you have located the JavaScript Settings menu option simply set them to “Yes” and that’s it. Simple right.

Magento Admin Panel

One other feature that can help to increase the loading time of a Magento site overall which again can be found within the admin panel is something called Magento Compiler.

Magento Compiler is a module that compiles PHP files that are usually located in different locations and moves them into one single file, which in simpler terms means that they are executed a lot faster because they are all in one place.

To find the Magento Compiler simple go to System > Tools > Compilation and once you’ve found it simply click on Run Compilation Process in the top right hand corner and you’re done.

Once these changes have been made it would be a good idea to run your site one of the tools mentioned above to see if these changes have made any difference. Once you have a fresh set of results it’s time to move onto the more advanced stage.

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

Step Four: 404 Errors

This is a very important final step. If you are running a large Magento website with lots of 404 errors, these must be 301’d to a relevant page.

If you do find you have a few 404 errors please just don’t 301 redirect them to your homepage. Please take the time and make sure that broken product pages are redirected to alternative or relevant products or category pages.

Google Webmaster Tools

Taking control of your 404 errors may not necessarily speed up your site that much but it improves the overall user experience of your website, keeping customers happy!

To 301 redirect in Magento simple add the following to your htaccess file:

Redirect 301 /broken-url http://www.example.com/new-url

Please note that you do not have to add the whole broken URL just simply everything after the trailing slash / .

Again please take care when adding a 301 to your htaccess and make sure that you test the redirected url to make sure it now redirects to the new required page.

If you are unsure exactly where or how to find any 404 errors that your website may have there are a number of tools available such as Screaming Frog or if you have access to a Google Webmaster account they can be found within that as well.

Final Magento Speed Test

Once all of the above have been added and configured it’s time to see what the improvements are by running another page speed test.

Final Page Speed Test

As you can see from the above screen shot, we still have a few bits and pieces to finalise but the immediate results are clear. This page has gone from a page speed of 38 to 90, by simply just applying some basic changes to htaccess file and within the Magento admin panel. 

Final Page Speed

We will continue to make changes to this site and it’s server configuration to get to the mid nineties and hopefully achieve a score of 100 or an A grade.

Other Ways To Speed Up Magento

It pays to play close attention to the number of extensions you have installed within Magento. The more modules or extensions you have the slower the website will load and it pays to carry out a bit of spring clean every now and then and remove extensions or modules you’re not using.

If however all this htaccess stuff scares you a little there are extensions out there that can do most of it for you however, I have never used them, so it wouldn’t be viable for me to say if they’re any good. 

Images

Make sure your images are optimised, no greater than 92dpi and a minimum of 72dpi and please make sure they are correctly sized before they’re added to your website. Try to also save your images as a .png format which will compress images without losing any quality.

Content Delivery Networks

Other advanced methods include a CDN or content delivery network and I personally would highly recommend the service from CloudFlair.com. 

I hope you’ve found this brief guide to speeding up Magento useful. I hope to add a few more Magento optimisation tips on our blog over the coming weeks so stay tuned for that.

With page speed being such an important factor and with large online retailers constantly looking for ways to reduce bounce rate , whilst also keeping  their user experience of their website as smooth as possible, it will certainly pay dividends to spend some time looking at your Magento sites overall page speed.

March 18, 2013
Matthew Weeks

Website: www.adurolabs.com

Follow Matt on Google+

3 Comments

  • mendenhall August 30, 2013 mendenhall

    I love this blog, always great content

    Comment Link
  • Mike June 12, 2013 Mike

    Exactly what I needed to see in an easy walk through article. Thanks for the help!

    Comment Link
  • Paul March 26, 2013 Paul

    This is exactly what I've been looking for. Thanks for sharing!

    Comment Link

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.