Book Image

Instant PageSpeed Optimization

By : Sanjeev Jaiswal
Book Image

Instant PageSpeed Optimization

By: Sanjeev Jaiswal

Overview of this book

PageSpeed is an important aspect of web design and site management. It is a Google measure of how well the site performs to technical measurements. PageSpeed can be measured using Google's own tool or a browser plugin. It is used to score sites in indices, and is important from a UI view as it forms a large part of the success of your site.Instant PageSpeed Optimization is a practical, hands-on guide that provides you with a number of clear, step-by-step exercises, which will help you to take advantage of the real power that is behind web optimization techniques, and give you a good grounding in using it in your websites.The book explores topics like HTML standards used for optimization, minifying scripts, and taking care of images, and solves the common errors that users do unknowingly. It will take you through a number of clear, practical recipes that will help you to take advantage of all the possible technologies to make your websitess efficient, fast, and accurate. You will not only learn basic standards to optimize your websites, but you will also learn some advanced level methods such as Apache handling, Flush methods, making AJAX cacheable, setting up browser caches, and reducing image size using CSS sprites. If you want to take advantage of all the necessary methods to make your website faster and efficient, then this book is a must-have.
Table of Contents (7 chapters)

Avoiding redirects and Not Found errors – 301 and 404 codes (Advanced)


You might have observed that you opened one URL, but were being redirected to another URL. Like when you try to open http://www.google.com/docs, you would be redirected to https://drive.google.com/#my-drive.

Sometimes it is necessary to do so due to various possible reasons as follows:

  • To show temporarily moved article with a hint that it has been moved temporarily

  • To show the content even if the URL is permanently moved through aliased URL

  • To display easily remembered URL

  • To add a trailing slash to make the website working fine if not configured properly on server side

  • To transfer URL from HTTP to HTTPS or vice versa according to the client's requirement

  • To display the domain name based on geolocation like typing google.com would redirect you to google.co.in if you are in India

Whichever the case may be, if you don't have any choice, go for the redirect option, else try to minimize it in your website.

If you have changed the content's URL recently using different rewrite engine rules or by any other way, try to fix the broken link, which will be a waste of time and we could lose users as well.

How to do it...

There are various ways to minimize or avoid redirects. Let's see how to avoid redirects as follows:

  1. If you know the exact URL for any content, type that even though it is a bit lengthy. Trust me, it would save your time, and the response would be much faster because latency period while redirecting the URL is not present there. The worst thing about redirecting a URL is that unless and until it is being redirected properly it will not render anything, not even a single line of that page.

  2. Sometimes a wrongly typed URL also redirects you to the correctly indented page like http://www.facebook.net would lead you to https://www.facebook.com/, so better type the correct URL, if possible.

  3. Try to type the forward slash (/) at the end of URL to render the default page properly like http://www.aliencoders.com/ to open the default page that is, http://www.aliencoders.com/index.html, which would otherwise give an error if it's not properly handled by the server. You may rewrite the rule to avoid redirect in such a case, so that the server automatically corrects the URL and serves the right resource for you.

  4. Many websites use redirect to log the user's information for web analytics; if it is necessary or required, use some other method, such as sending a request through JavaScript, which should be negligible and the user would not notice the latency. For example:

    img.src = "http://www.example.com/logs/small.gif?page = " + encodeURI(thisPage) + "&ref = " + encodeURI(referPage);
  5. Now to avoid showing bad requests or repair broken links, it is quite obvious that URLs should be mentioned from time to time or article's reference would not work, because of a broken link, which may happen due to removal of such content or moving of such content somewhere else. We should check broken links and fix them, if we find any, from time to time. Top 'page not found' errors at www.aliencoders.com with the count is shown in the following screenshot:

How it works...

When you pass any previously mentioned URLs for redirection, then it is either already mentioned at the server sided with the help of rewrite engine rule or it uses the http-equiv="refresh" mechanism at the client side. As I always insist, try to avoid redirects; but if it is worthy to use redirects, use the server-side techniques to use redirects.

When you open any webpage, which is moved permanently or not available over the Internet, you would get either 404 (Not Found ) error or 410 (Gone) error as the server response. If you are not looking over these two issues seriously, you are surely playing with the user's emotions for which you have to pay by losing them permanently.

There's more...

Wherever possible, you should update the links to resources that have moved, or delete those links if the resources have been removed. Avoid using HTTP redirects to send users to the requested resources, or to serve a substitute suggestion page. As previously described, redirects also slow down your site, and are better to avoid as much as possible. Use Google Analytics or any such famous analytics to track down all these errors to avoid it as much as possible.