You have probably seen the warning to serve resources from a consistent URL in GTMetrix or a similar service. The warning sounds something like: “The following resources have identical contents, but are served from different URLs. Serve these resources from a consistent URL to save 1 request(s) and 934B.”.
This post will help you understand the issue and solve it.
Why Does it Happen?
Websites use resources, such as images, CSS and JS files to deliver the desired user experience, including graphics and effects. Each of these resources gets “called” by the browser, whenever a user accesses a web page. This is called a HTTP request.
In most cases, each resource has a single URL and requires only one HTTP request. Sometimes, however, a resource can be served from multiple hostnames or domains, resulting in multiple URLs to a single resource. This happens most often when resources are shared between two or more pages of the same website or between different linked websites. Sometimes the issue is caused by plugins, using the same external libraries or other resources. It could also be a result of misconfigurations in your CDN settings.
In such cases, a resource can, depending on its configuration, get duplicated. Because the same resource is available at two different URLs, every time the page is accessed by a user, an additional HTTP request is used to call it. If the resource is available at multiple domains, an additional DNS lookup is also triggered.
The additional HTTP request slows down your pages because what really happens is that the resource gets downloaded twice per session.
How to Serve Resources from a Consistent URL
There are 4 most common situations, resulting in duplicated resources: when a website has both a http and https version or www and naked domain version; when a.mywebsite.com and b.mywebsite.com share resources; when plugin 1 and plugin 2 use the same resources; and when resources are hosted on a CDN but are still accessible on the original URL.
Most often, the issue with inconsistent URLs is caused by resources, such as images, shared by multiple pages or linked sites.
For example, if your website mywebsite.com shares an image with the site division subsite.mywebsite.com, which it links to, you can end up with the following paths to the same file:
This causes inconsistency and results in additional HTTP requests.
To resolve the issue, serve the file from a single hostname. This means that, instead of hosting the same file on the hostname or domain of each site as in the example above, you host it on, for example, mywebsite.com and link to it from subsite.mywebsite.com.
This solution works also for different domains, as long as they are linked. Even though this will require a DNS-lookup, the benefits of caching would minimize its negative impact. It’s also worth to note that a DNS-lookup will anyway be required due to the fact that the two sites are linked.
Sometimes, using a CDN to speed up your site can also result in a few duplicated resources. This can be due to your setup. Usually, you “move” all your media files to the CDN. However, it can happen that your theme “pulls” some files, such as the site’s logo directly from the media library. Thus, you end up with the same file, served from the CDN (the media library version) and from your domain (the file pulled into the theme’s settings).
If this is the case, you have to manually change the file path in your theme’s files to point to the file on the CDN. Even though this can differ from theme to theme, in most cases you can find the logo settings in the file, named header.php.
Find the line, defining the path to the logo file and edit it to match your CDN resource URL. NB! Changes to theme files require the use of a child theme!
301 and 302 Redirects
If your site is available both in HTTP and HTTPS version, which is always the case when using a SSL certificate, all your resources are likewise accessible both via HTTP and HTTPS. Even though the content and the rest of the URLs are identical, these versions are read as different URLs. This causes URL inconsistency issue.
You end up in the same situation if your site’s setup makes your site accessible both at www.mysite.com and mysite.com. Even though the two seem identical, in reality you have two copies of the site: one hosted at the root of the domain and one hosted at the subdomain www. As a result of having a duplicated site, you also end up having a bunch of duplicated resources.
In the case of multiple website versions, you are urged to choose a preferred version and implement 301 redirects. Using 301 redirects sitewide instructs search engines and browsers to skip all other versions (they are marked as permanently moved) and go directly to the preferred version of pages and resources.
This is the only sure way to handle this kind of issue and best SEO practice.
If you aren’t sure, whether the preferred version should be kept or if your website is still developing and changing, you can choose to point to currently preferred content by using 302 redirects. 302 signals that resources have temporary been moved.
No matter which method you choose, you have – at least temporarily – solved the issue with inconsistent URLs. Just remember that only 301 redirects pass link juice to the destination page.
You can also consolidate resources by implementing canonical tags. This is especially useful in e-commerce or similar setting, where one resource is referred to by multiple pages using different categories or attributes.
Canonical tags should never be your first choice, as long as you can implement 301 or 302 redirects. The reason is that they have proved to be problematic in terms of SEO. You can however use them in all cases, where 301 or 302 redirects can’t be applied.
You implement canonical tags by adding the link to the preferred resource with rel=canonical attribute in the code of the not-preferred identical version. rel=canonical can be used in the same way across domains.
If you can’t manually edit the code of your pages, try a plugin, such as Yoast SEO (premium version), which offers easy management of rel=canonical.
Sometimes you can get a warning to serve resources from a consistent URL as a result of multiple plugins sharing resources or a single plugin adding a string (?dshd=343 or similar) to the original URL.
In the case of plugins sharing resources, you might need to solve the problem by deactivating one of the plugins causing troubles. The reason is that plugins are typically a third-party product, where you don’t have control of resource handling.
In the case of plugins adding strings to URLs, try this fix to remove query strings from static resources.