What if I told you that two lines of code could wipe a significant chunk off your page load speed and speed up every page on your website? I’d have your attention right? Everyone wants their website pages to load fast. It’s good for visitors. Therefore it’s good for business. It’s good for Google, which penalises slow loading sites. Therefore it’s good for business. There are vast numbers of online guides showing, in huge intricate detail, how to decrease page load time. Just reading them takes time and effort and then implementing the advice takes more time and effort. On top of that time and effort, there’s money, because using a decent web server is fundamental. However, there’s something that I never see mentioned in any of these guides and it relates to sites that are using https with an encryption certificate. So if that’s your site read on, and if you aren’t using it, then read my guide showing why you should be using https.
Here’s the GTMetrix waterfall report for my site before the change:
Notice the half a second (508ms) attempt to render that HTTP version of the site before doing a 301 redirect (the top row)? That’s what I’m going to eliminate.
Here’s the same report, but after the change:
Half a second saved! Which is about 20% of my total page load time.
If you tested your site and it does that HTTP rendering step, then here’s the magic:
Apache webserver
Edit or create your .htaccess file (found in the root folder of your website) and look for a line that says: “RewriteEngine On”
Just after that, paste the following code, but MODIFY THE URL TO MATCH YOURS, otherwise your site will immediately break!
# force SSL (remember to change site address below)
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R=301,L]
OK, yeah, that’s 3 lines of code, but the first line is just a comment. If the “RewriteEngine On” bit isn’t there already, add that just in front of it too.
Nginx webserver
Edit your Nginx config file (e.g. nginx.conf)
Add this before anything else that looks like redirects:
# force ssl (remember to change site address below) server { listen [::]:80; listen 80; server_name yourdomain.com www.yourdomain.com; return 301 https://www.yourdomain.com$request_uri; }
A couple more lines of code, but same result. If you are using the Nginx panel for cPanel, there is a button to enable this.
Any time you share a link to your site, you should be specifying the https version, but for people that are typing in the URL manually, or people clicking old backlinks before you ever had https, then this will speed up the pageload for those visitors.
Please test changes like this thoroughly before assuming what works for me will work for you.
Using Cloudflare you already greatly reduces the loading time, I have done tests with these codes and almost did not alter the speed. And I also prefer to use webpagetest, if you do not know, follow the link: https://www.webpagetest.org/ .
It’s much more reliable than the GT Metrix, in my opnion.
You can see the speed of my site: https://mundodoido.com/
Good tip with webpagetest. Thanks! And yes, Cloudflare is great – I’ve temporarily disabled it while testing some local caching, but normally use it. There’s an option in Cloudflare to automatically do the same thing that the code above does. Also, anyone using Nginx with Cpanel will also have a server level switch for this 🙂 Your site is loading nice and fast.