Easy Way to Serve Resources from a Consistent URL

Overview

It’s important to serve a resource from a unique URL, to eliminate duplicate download bytes and additional RTTs.

You’ve probably seen the warning to serve resources from a consistent URL in GTMetrix or a similar service. The notice reads as follows: “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 674B.”.

This post will help you understand the issue and solve it.

Why Does it Happen?

In most cases, each resource is served from a single URL and requires only one HTTP request. Sometimes, however, a resource is available through multiple hostnames or domains. As a result, every time the page is loaded, the resource gets called and downloaded multiple times – from each individual URL. This causes the page to load slower, affecting SEO negatively.Websites use resources, such as images, CSS, and JS files to deliver the desired user experience, including layout and functionality. Each of these resources gets “called” by the browser, whenever a user accesses the web page they are attached to. This is called a HTTP request.

Serving identical resources from multiple URLs happens most often, when such resources are shared between two or more subdomains or subdirectories 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 redirects. Lastly, 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 page is accessed by a user, an additional HTTP request is required in order to call the resource. If it is hosted at separate linked domains, an additional DNS lookup is triggered as well.

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.

Move

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, you can end up with the following two paths to the same file:

/media/image.png

/subsite/media/image.png

This causes inconsistency and triggers an additional HTTP request every time the parent-page is requested.

To resolve the issue, you should serve the file from a single hostname. This means that, instead of hosting the same file on the webshite and its subdivision, as in the example above, you should 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.

Rewrite

Sometimes, using a CDN to speed up your site can also result in a few duplicated resources. In such cases, the issues is caused by the way your theme or plugins handle static resources. Usually, you “move” all your media files to the CDN by editing all static resources’ URLs manually, by creating a C Name or via a plugin.

While this works flawlessly most of the time, 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 applied to theme files require the use of a child theme!

The best way 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). Please enter the full URL-path to the duplicate resource (one resource at a time)! If you fail to include the full path to the particular resource, you risk to edit all of your site’s permalinks, causing it to crash!

Redirect

If your site is available both at www.mysite.com and at mysite.com or you host different variations of your site on the two addresses, this can easily result in duplicate resources. Even though you might have practical reasons for constructing your site this way, in reality you end up having two websites: one hosted at the root of the domain and one hosted at the subdomain www. As a result of having two websites that are so deeply connected, chances are that you’ll end up with 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 in the same time 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. Both 301 and 302 redirects pass link juice to the destination page.

Canonical Tag

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. 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.

Plugins

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 the issue. 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 check 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.

Anjali Punjab

Anjali Punjab is a freelance writer, blogger, and ghostwriter who develops high-quality content for businesses. She is also a HubSpot Inbound Marketing Certified and Google Analytics Qualified Professional.