Avoid using redirects.


When your web pages contain too many (sometimes one is already bad enough) redirects it can decrease your page speed significantly. It’s recommended examining your website and checking & removing any redirect that isn’t absolutely necessary.

What is a redirect?

A redirect is a function which automatically which makes it possible to visit one page or file to get to another. This means the user does not directly connect to the page or file it should go but first has to go through another connection (or multiple connections) to get to the correct page or file.

How do they slow my site down?

When using a redirect, the browser of the user does not directly connect to the destination the user eventually goes to. It first has to connect to the redirect before it reaches the final destination. This slows down the process of reaching the final destination because the user reaches it indirectly instead of directly.

Avoid all redirects – except for some

If possible, do not use any redirection methods at all on your web pages. Sometimes, however, you just can’t go without them. One example is the 301 non-www to www redirection most websites use nowadays. This redirection is often necessary if only because it tells Google and other search engines which URL (www or non-www) they should take as your domain’s default URL.

Types of redirects

301 & 302 redirects – These are both server side redirects which indicate the page has permanently(301) or temporarily(302) moved. If you have to use a redirect, this is the fastest type of redirection to use. These types of redirects are also usually used for (non-)www redirection.
PHP script redirect – Redirect by using a PHP script. The best option if you can’t use 301 & 302 redirects.
Javascript redirect – Use a client side Javascript to redirect. Not recommended.
HTML redirect – A client side HTML code you can use to redirect from a HTML page. Not recommended.

Detect your redirects

Redirects do not only come in the form of page redirects. You can also have redirects inside your HTML, CSS or Javascript code used to point to use or import other URL’s or files. The Broken Requests tool can detect all the 301 and 302 redirects pointed to on your web page.
If you want check whether your actual URL uses a redirect you can use the HTTP Header Checker tool to check the HTTP status code. Type in your URL, click the button and see whether your web page has a redirect and where it redirects to. If you get a “HTTP/1.1 200 OK” code it means your URL is not redirected. If you get a HTTP/1.1 301 Moved Permanently or a HTTP/1.1 302 Moved Temporarily code it means your web page is being redirected.

Fix your redirects

As stated above, you should remove any redirect which isn’t absolutely necessary. The ones which are necessary should always be server side(301 or 302 redirects) and never client side(HTML, Javascript, etc..). So when your website uses client side redirects replace them with server side redirects.

How to make a 301 or 302 redirect

You can edit your .htaccess file to make these types of redirects. Below I’ve illustrated some examples of codes you can use:
Permanently redirect from page in directory to other page:

Redirect 301 /directory/example.php http://www.exampledomain.com/examplepage.html

Temporarily redirect from page to other page in directory:

Redirect 302 /example.html http://www.exampledomain.com/directory/examplepage.php

To redirect from non-www to www (site wide):

RewriteEngine on RewriteBase / RewriteCond %{HTTP_HOST} !^www. [NC] RewriteCond %{HTTP_HOST} ^(.+)$ [NC] RewriteRule ^(.*)$ http://www\.%1/$1 [R=301,L]

Edit these codes to your satisfaction and add them to .htaccess file and save it to the root directory of your website where the .htaccess file should be. Remember, only use these redirects if you really have to. It’s preferred to not use any redirects at all if you care about speed (but sometimes they can’t be avoided).