Redirect HTTP to HTTPS for Better Page Speed and SEO.

A slow website is a drag, literally!

My own website became very slow recently.   So I used Google Page Speed Insights Tool, to diagnose what was wrong.  As a result, I had to fix quite a few issues. 

One issue, I had never come across before, was “Avoid too many Landing Page Redirects”.   This was causing my site to slow down by as much as 15 seconds!

How to redirect HTTP to HTTPS the correct way for SEO and page speed

Canonical URL's And Redirect Issues

The reason for the landing page redirects, was an issue with my websites Canonical URL’s.  Canonical URL’s are important and must be used in the right way, to avoid duplicate content issues with Google and SEO ranking.  Google penalizes duplicate content, which is defined as multiple pages with the same URL, or multiple pages with similar content.  

My understanding was there was only 4 different types of similar URLs on a website.  I made sure my website redirected from:

  1. www to non-www, 
  2. http:// to https://

However, I was missing 1 crucial bit of knowledge:  The last version of any URL, has a trailing slash at the end ‘/’.  

This makes a total of 8 different versions of a given URL, as seen in the example below.

  1. http://domain.com/blog
  2. http://domain.com/blog/
  3. http://www.domain.com/blog
  4. http://www.domain.com/blog/
  5. https://domain.com/blog
  6. https://domain.com/blog/
  7. https://www.domain.com/blog
  8. https://www.domain.com/blog/

I had missed the trailing slash at the end, and because of that, my website redirected 3 times taking 15 seconds!  

Once I realized what the issue was, I was able to not only solve the issue, but improve the .htaccess so that it only has 1 redirect.  This improved my page speed by 11 seconds.

Since I was able to get such a boost from it, I thought it was a great idea to share with you.  Let’s go over the correct way to write the http to https force redirect, to improve SEO and Page Speed.

There are 8 different versions of your websites URLs, so make sure you redirect to HTTPS the right way!

http to https: The incorrect way

First, let me share the incorrect way.  This is what I had in my .htaccess file prior, that was causing the redirect chain issues, and slowing down the website.  This was written by Really Simple SSL plugin, as I have a different script when done manually.

				
					<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
				
			

Normally I would add the following to my .htaccess file.  On WordPress Sites, do not add the extra RewriteEngine On, you would just put in lines 2 and 3 directly under the one that’s already there.  

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

However, this also creates an extra redirect.  2 redirects aren’t that bad, but when it comes down to making a website competitive, it needs to be fast on mobile, and you want to retain all your link juice.   

Each redirect (Google states they will stop crawling after 5 redirects) takes up the link juice that would have originally went to your page rank.  

So then what is the correct way to write the http to https redirect?

The Correct Way to redirect http to https

The correct way to redirect your website to https has 2 primary benefits.  

  1. Saves visitors valuable seconds when loading your page.  
  2. Saves more link juice for your page ranks.  

When it comes to SEO, the seconds matter a lot.  If your site loads in 4 seconds or more, you are losing many valuable would be visitors.  

Therefor, it is extremely important you do this the correct way!  For starters, you need to know what type of server you are on.  

So here is the correct way to redirect http to https on Linux Servers or Hosting:

Step 1: To start, make sure you have the Rewrite Engine On part.  

				
					   RewriteEngine On
				
			

Step 2: Check for the trailing slash on non-filepath URLs.

Remember the rule, browsers and servers place a trailing / at the end of the directory URLs, but they leave the / out at the end of files.  We want to continue following that standard.

We also want to determine if the URL ends in a trailing slash.

				
					 RewriteCond %{REQUEST_URI} /(.+)/$
 RewriteCond %{REQUEST_FILENAME} !-d
				
			

First, this checks: 

  • A) This is a URL
  • B) This URL is NOT root
  • C) This URL ends with a forward slash.  

If all conditions are true we implement the following rewrite rule.

				
					RewriteRule ^ https://domain.com/%1 [R=301,L]
				
			

This RewriteRule takes the URL from the capture (.+) and appends it to the domain. You can see that the site URL does not include www, and has https://

Step 3: We need to enforce a trailing slash policy on directories.

				
					RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
				
			

The first line checks to make sure we are working with a path, not just the root domain. It also ensures the request ends in a trailing slash.

The second line makes sure we are working with a directory. This is important. If we left this part off we would have an endless redirect loop.

				
					RewriteRule ^(.+)$ https://domain.com/$1/ [R=301,L]
				
			

Our RewriteRule takes the URL path requested and appends it to the domain followed by a trailing slash. The capture group (.+) captures the path only if there is one or more characters. This ensures we don’t place a second / on the website root.

Step 4: We need to check to see if www. is included in the requested URL and https is the protocol or scheme used.

				
					RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{https} off
				
			

The first line checks to see if our www is included.

It is important to use the no case [NC] flag.  If you do not use the no case flag it may redirect on Www. since the first letter is uppercase and does not match.

The second line checks the protocol to see if it is https.

				
					RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
				
			

Finally, our last RewriteRule takes the requested path and appends it to the domain with the www and https added.

For this one, we use the capture group (.*) which captures zero or more characters. This ensures we redirect any request to the website root that does not have www and https.

Putting it all together. It should look like this.

				
					#### Force HTTPS:// and remove trailing / from files ####
## Turn on rewrite engine
RewriteEngine on

# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://domain.com/%1 [R=301,L]

# Include trailing slash on directory 
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://domain.com/$1/ [R=301,L]

# Force HTTPS and non-WWW 
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{https} off  
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
				
			
Scroll to Top