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 subdomains or subdirecotries of the same website or between different linked websites. Sometimes, the issue can also be caused by two or more plugins, using the same external libraries or resources. In other cases, it is a result of incorrectly implemented SSL encryption. It could also be a result of misconfigurations in your CDN settings or a bug in your theme.
In such cases, resources get duplicated. Because the same resource is available at two different URLs, every time the parent-page is accessed by a user, an additional HTTP request is used to call it. If the resource is available at separate linked 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 and are accessible both via the CDN and the origin server.
Most often, the issue with inconsistent URLs is caused by resources, such as images, shared by multiple subdomains 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 triggers an additional HTTP request every time the parent-page is requested.
To resolve the issue, serve the file from a single hostname. This means that, instead of hosting the same file on 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 separate domains – and makes sense to apply, when they are linked. Even though it will require an additional DNS-lookup every time, the benefits of caching would make it worth it. 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. The issue is in these cases caused by your setup. Usually, you “move” all your media files to the CDN by editing all static resources’ URLs manually or via a plugin. However, it can happen that your theme or a plugin “pulls” some files, such as the site’s logo, into the site’s code and out of 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 change the file path in your theme’s files to point to the file on the CDN. You can try to do it manually (not optimal). 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!
Your best choice when you need to make sure that all URLs are consistent, that is the ones found in diverse files, on pages, and in the database, is to use a plugin as Better Searcch and Replace. The reason is that it finds all entries maching your key phrase and replaces them effectively (they can be many).
If your site is available both in HTTP and HTTPS version, which is the default 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 at mysite.com. Even though the two are identical and you probably think it is convenient this way, 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 duplicated websites, 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 it is also 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, as they aren’t a real solution to the problem. The reason is that they don’t really eliminate the presence of duplicate resources. They simply instruct search engines to prefer certain version of the content. 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, without rewriting the plugin.
In some cases, it can help to try turn on and off diverse plugin settings and see whether this doesn’t solve the problem. While this isn’t a true solution to the URL inconsistency issue, turning a setting on or off can affect plugin behavior by eliminating the use of the duplicated resource.
In the case of plugins adding strings to URLs, try this fix to remove query strings from static resources.