How can I reduce multiple redirects on my website?

Hello,

I have a WordPress website that is hosted on Hostinger’s shared server with hPanel interface.

I am facing multiple redirects when typing http://www and it is using two redirects to reach to https://non-www. First it goes to https://www and then goes to the https://non-www version. This is increasing my website’s aggregate TTFB because each redirect is taking 500-800ms time.

I want to avoid the extra redirect of https://www

So basically what I want to achieve is this:

http://www.domain.com - > https://domain.com without any extra redirect
http://domain.com - > https://domain.com
https://www.domain.com - > https://domain.com

Any requests http or https with or without the www will go to https://non-www

How can I achieve this and reduce my website’s TTFB?

Many thanks

1 Like

It looks like, by default, Wordpress does two redirects for http://www.example.com -

I agree that’s really broken, and there are two things to note. Firstly, if your WordPress site canonical URL on the Admin > Settings > General page is set to “https://example.com”, you’ll only get one redirect from http://example.com - that is, make sure example.com is used everywhere your website may be advertised.

In Chrome now*, if you just enter “example.com”, it defaults to http://www.example.com. I believe that Chrome is about to change to defaulting to “https://example.com”.

  • not sure about the timing and believe this about to change any day now.

So, most of the time, in actual practice, this isn’t even a problem, as there will be one redirect only, especially as Chrome switches to a default of https. It’s important you run your Webpagetests with a URL of “https://example.com” to avoid including redirect times in your tests, and thereby causing confusion.

The second point worth making is that this is easily fixed with explicit rules in your .htaccess file. I suspect you’ll need to craft them manually, just add them. The main thing you need to do is to add a redirect from “http://www.example.com” to “https://example.com” - that is, a single redirect to get in ahead of the default double redirect. There’s no point in adding in a redirect for the https://www.example.com case as WordPress’s default will handle this normally.

Some redirect examples from stackoverflow (one example; there might be better methods but this is simple):

I hope this helps!

ps: another possible .htaccess solution - this redirects anything that is not www.example.com to www.example.com so would need tweaking:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

ALSO - worth noting - the TTFB for your site only comes from testing your site with its correct URL. If you are testing with non-correct URLs, you’re not seeing the real TTFB. Always test with the correct, final, official URL - ie the one in Admin>Settings>General.

2 Likes