1

Just reading an question about Absolute vs Relative URL and read some articles about same. Creating a great confusion, as both have there advantages and disadvantages.

I would like to know what would be better to use relative or absolute urls on http(s) websites? (for pictures, CSS files, JS files, etc.)

Impact on page load speed?

Impact on SEO?

Impact of many external or internal?

Community
  • 1
  • 1
Vineet1982
  • 7,730
  • 4
  • 32
  • 67

3 Answers3

2

Relative URLs are always the best. Absolute URLs may help when you use CDN or send a HTML code in emails.

Absolute URLs load the content via your web server through http where as relative files in PHP include() and require() are loaded internally, thus increasing page load speed.

Using relative urls in PHP or any other server side script also allows you to govern them via your server configuration and thus, improving security.

Don Joobb
  • 31
  • 3
2

Short Answer

what would be better to use relative or absolute urls on http(s) websites?

For a developer, relative URLs are best for internal resources, and absolute URLs are your only option for external resources.

As the other answers and comments point out, your development environment will probably have a different file directory structure than your deployment (production) environment, and either may change from time to time. If relative URLs are used in development, such a change in your directory structure will have a little or no impact on your project views.

Explanation

Impact on page load speed?

None. The impact on page load speed is affected by the number, size and location of the resource files referenced. How you reference them is immaterial, because every resource still has to be requested from the web server, which must then feed the response (content) back to the browser for use. It does not matter if your URL is ../assets/images/logo.png (relative) or https://example.com/assets/images/logo.png (absolute). The browser still has to go there and get it. This is the part that costs you time.

Impact on SEO?

None. And for the same reason. The destination (location) is the same in either case, and the search engines know this instantly, just like a web browser does.

Impact of many external or internal?

The impact of too many (or too few) links to a resource on your SEO is a matter of further discussion and study. For our purposes here, I want to point out that there is no such thing as a relative URL to an external resource. Further, the impact on SEO will not likely be any different for internal resources on the basis of its relative versus absolute references (URLs).

0

For production, I prefer using absolute URLs for the backend APIs specially if you are using several containers and something like NginXis routing it. Otherwise your backend servers may return HTTP error status.

marcg
  • 552
  • 1
  • 8
  • 20