Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast Loading web pages

We are planning to develop a new website. Our goal is to load web pages quickly. What are all the techniques we need to follow.

Can anyone give me good suggestions, forums links or articles.

Our platform is PHP, MySQL, Javascript, and AJAX.

like image 788
panidarapu Avatar asked Feb 25 '09 06:02

panidarapu


People also ask

What is a fast load time for a website?

Ideally, you'll want your website to load within three seconds, or two seconds if it's an ecommerce site. The two-to-three second mark is the turning point where bounce rates skyrocket – in fact, 40% of consumers will wait no more than three seconds before abandoning a site.

What causes slow loading web pages?

Slow site speeds can result from network congestion, bandwidth throttling and restrictions, data discrimination and filtering, or content filtering. If you notice slow speeds when visiting your site, you can run a traceroute between your computer and your website to test the connection.


1 Answers

1. Enable Keep-Alive

HTTP Keep Alive refers to the message that’s sent between the client machine and the web server asking for permission to download a file. Enabling Keep Alive allows the client machine to download multiple files without repeatedly asking permission, which helps to save bandwidth.

To enable Keep Alive, simply copy and paste the code below into your .htaccess file.

<ifModule mod_headers.c>
    Header set Connection keep-alive
</ifModule>

2. Disable hotlinking of images

When other website’s ‘hot link’ to your images it steals bandwidth, slowing your site down. To prevent other sites from hogging your bandwidth, you can add this snippet of code to your .htaccess file. Remember to change the bit that says your_Domain_name.com!

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?your_Domain_name.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]

3. Compress your website with gzip

Gzip is a simple method for compressing your website’s files to save bandwidth and speed up page load times. Gzip works by compressing your files into a zip file, which is faster for the user’s browser to load. The user’s browser then unzips the file and shows the content. This method of transmitting content from the server to the browser is far more efficient, and saves a lot of time.

You can enable Gzip by simply adding the following code into your .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:

SetOutputFilter DEFLATE

To check whether Gzip is enabled or working properly on your site, you can use Gziptest.com.

4. Enable Expires Headers

Expires headers tell the browser whether they should request a specific file from the server or whether they should grab it from the browser's cache.

The whole idea behind Expires Headers is not only to reduce the load of downloads from the server (constantly downloading the same file when it's unmodified is wasting precious load time) but rather to reduce the number of HTTP requests for the server.

So in .htaccess file include the following things

<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 plus 1 month"
    # Javascript
    ExpiresByType application/javascript "access plus 1 year"
</IfModule>

5. Replace PHP with static HTML where possible

PHP is great for making your site efficient and reducing the need to enter the same information multiple times. However, calling information through PHP uses up server resource and should be replaced with static HTML where it doesn’t save any time

6. Specify a character set in HTTP headers

For the same reason as above, it’s useful to specify a character set in your HTTP response headers, so that the browser doesn’t have to spend extra time working out which character set you’re using.

You can do this by simply adding a UTF-8 character set tag in your website’ssection.

7. Enable Output Compression

The compression can be done in two ways.

Apache actually has two compression options:

  • mod_deflate is easier to set up and is standard.
  • mod_gzip seems more powerful: you can pre-compress content.

Your two options for file compression are Deflate and GZIP.

  • Deflate is an option which comes automatically with the Apache server and which is simple to set up.
  • GZIP on the other hand needs to be installed and requires a bit more work to install. However, GZIP does achieve a higher compression rate and therefore might be a better choice if your website uses pages which have a lot of images or large file sizes.

Deflate is quick and works, so I use it; use mod_gzip if that floats your boat. In either case, Apache checks if the browser sent the “Accept-encoding” header and returns the compressed or regular version of the file. However, some older browsers may have trouble (more below) and there are special directives you can add to correct this.

zlib.output_compression Whether to transparently compress pages. If this option is set to "On" in php.ini or the Apache configuration, pages are compressed if the browser sends an "Accept-Encoding: gzip" or "deflate" header.

PHP Default: Disabled

In Php.ini

zlib.output_compression = On

If you can’t change your .htaccess file, you can use PHP to return compressed content. Give your HTML file a .php extension and add this code to the top:

In PHP:

<?php 
    if (substr_count($_SERVER[‘HTTP_ACCEPT_ENCODING’], ‘gzip’))
    ob_start(“ob_gzhandler”); else ob_start(); 
?>

This section will turn on the apache mod_deflate module, which compresses text, css, and javascript before it is sent to the browser. This results in a smaller download size. Enable it in .htaccess file so that it looks like the following:

<IfModule mod_deflate.c>

############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip

    # Insert filter on all content
    SetOutputFilter DEFLATE
    # Insert filter on selected content types only
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/x-javascript text/xml application/xml application/xhtml+xml image/x-icon image/svg+xml application/rss+xml application/x-font application/x-font-truetype application/x-font-ttf application/x-font-otf application/x-font-opentype application/vnd.ms-fontobject font/ttf font/otf font/opentype 

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

**

8. Enable cache, OPcache, and eAccelerator (Another PHP Caching tool)

Memcache is particularly useful for reducing your database load while bytecode caching engines like APC or OPcache are great for saving execution time when scripts get compiled.

9. Take Advantage of Native PHP Functions

Wherever possible, try to take advantage of PHP’s native functions instead of writing your own functions to achieve the same outcome. Taking a little while to learn how to use PHP’s native functions will not only help you write code faster, but will also make it more efficient.

10. Cut Out Unnecessary Calculations

When using the same value of a variable multiple times, calculate and assign the value at the beginning rather than performing calculations for every use. If you’re looping through an array, for example, count() it beforehand, store the value in a variable, and use that for your test. This way, you avoid needlessly firing the test function with every loop iteration.

11. Use the Strongest Str Functions

While str_replace is faster than preg_replace, the strtr function is four times faster than str_replace.

12. Stick With Single Quotes

When possible, use single quotes rather than double quotes. Double quotes check for variables, which can drag down performance.

13. Try Three Equal Signs

Since “= = =” only checks for a closed range, it is faster than using “= =” for comparisons.

14. Use isset( )

when compared to

count( ), strlen( ) and sizeof( ),

isset( ) is a faster and simpler way to determine if a value is greater than 0.

15. Cut Out Unnecessary Classes

If you don’t intend on using classes or methods multiple times, then you don’t really need them. If you must employ classes, be sure to use derived class methods as they are faster than methods in base classes.

16. Close Database Connections

Un-setting variables and closing database connections in your code will save precious memory.

17. Limit Your Database Hits

Making queries aggregate can reduce the number of hits to your database, which will make things run faster.

like image 135
Nɪsʜᴀɴᴛʜ ॐ Avatar answered Nov 15 '22 14:11

Nɪsʜᴀɴᴛʜ ॐ