I was using pingdom to test the load speed on a site I’m working on and got different results for www vs non www. WordPress automatically redirects to either www or non www depending on the settings.
For the site in question its set to use non www and when tested in pingdom it came back with a 1 second result. Shortly after that I tested the site again with pingdom now with www and it came back as 3 seconds.
I contacted my host (hostgator) cause I thought it might have been a weird dns issue and to be honest I’m not super clued up on dns related issues.
They told me that the redirect happens while wordpress is already loading so when I go to www it loads wordpress then redirects so it almost has to load the site twice. They suggested I use a standard htaccess redirect at the top of the htacess file.
After doing that I tested the site again with both versions and they both seem to load at around 1 second.
Now finally after all that my question is why does this happen? At what point does the wordpress default redirect kick in. Should I just by default add the redirect myself to the top of the htaccess file for all sites?
Basically just looking to find out more about how wordpress handles that redirect, if someone would perhaps link me to a codex page about it or a blog article that would be amazing.
You ran into a feature of WordPress known as Canonical Redirect. The idea is that each page on your website will only have one URL. But since it is possible to get to a page with a different URL, it will force a redirect to the “true” URL. The reason for this is to prevent duplicate results in search engines because of different URLs.
Some common cases of alternative URLs are:
/
example.com/?p=133
instead of the pretty URLexample.com/my-blog-post/
As for the domain name, I would force it from the
.htaccess
file instead of WordPress. It is a good idea to have the canonical URLs and Apache will handle it faster than WordPress with regards to the domain name.More information on it:
redirect_canonical()
function