5

Every time im about to put my local project into staging/production environment im beeing hardly reminded that WordPress saves all the urls as absolute.So i need to rewrite all the urls with a SQL-query, or use something like Search-and-replace script.

But why is WordPress doing this?

What is the benefits?

Is it a must for WordPress to work correctly?

There are themes like Roots where there are functions like roots_root_relative_url using preg_replace to remove the full url.

Wouldn't it be better to only have relative urls, what is the downsides of that?

The benefits of Relative urls in the database can be: Easy migration from local to stage/production on all links, attachments and so on. Cleaner, shorter urls in html.

But, Because WordPress doing absolute urls i sure that its good, im only want to now why and why not.

6
  • I am not sure why WP is doing that, but from an SEO perspective, absolute URLs are preferred. You can read this post by Yoast to know more about this subject.
    – Ahmad M
    Feb 8, 2013 at 16:30
  • Thanks Ahmad, Thats funny, im just started reading this. Very good article! But do you agree or disagree that it would be nice with relative urls for development? cus where should the line be for seo/development? Just want others perspectives :) ! Feb 8, 2013 at 16:35
  • Of course it would be easier to use relative URLs when writing code, but for me I do give a lot of weight to SEO consideration and I think that is best practice. Besides it is not really that hard to do DB search and replace as you mentioned. In fact, I use plugins like Duplicator to perform that task for me.
    – Ahmad M
    Feb 8, 2013 at 16:41
  • 1
    No its not that hard to make a Search and replace within SQL, but that can be pretty scary if there are plenty of data that needs to be rewritten every time you working in local/stagin envoirment. And if you want/cant relay on a third-party-plugin that doing the magic for you. Feb 8, 2013 at 16:56
  • 2
    Wordpress has functions to generate full urls, so storing them in the database for SEO is complete bullshit. Aug 19, 2019 at 22:36

4 Answers 4

4

If there were only relative URLs, what would they be relative to? A post can be viewed in several different contexts, including a feed, and a WordPress install can move into a different directory relative to root. There are issues with doing it either way, absolute URLs just give you a more concrete starting point from which to make changes.

2
  • Aaah thats true! But wouldn't it be good to have relative URL:s within content, like attachments, internal links and so on? and the siteurl intact? Feb 8, 2013 at 16:53
  • 2
    No, because the relative URL can change as well when you change permalinks and such. If you're going to have to do search-replaces anyway, then the absolute URL is easier to search on and replace on.
    – Otto
    Feb 8, 2013 at 17:02
1

Storing absolute URLs to your own domain makes the data structure less modular and favors the loss of long-term data integrity. During frontend output relative stored paths could still be processed to absolute ones by simply adding WP_SITEURL to it if needed. This way transferring a database to a different Server/Domain could be archived by only updating the WP_SITEURL via config file. String replace on sql files to change the domain is messy and can lead to unwanted behaviours.

1
  • 2
    How is this an answer? You're actually making the same point as the OP. WordPress unfortunately stores any links with the domain name. The question is still valid. Why does wordpress do that?! Feb 16, 2020 at 20:24
1

I guess the right answer is: there is no technical reason for this.

It's pure legacy. It once was like this, then they had to preserve retro-compatibility, they found no other way (maybe didn't even really look for), and kept it.

Wordpress is not designed for developers.

2
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Apr 2 at 16:42
  • You're right, I'll try to find more "hard info" on this. I was just really pissed off about it when I wrote this "i guess" answer (which I still maintain 100%)
    – theredled
    Apr 2 at 20:18
0

Since this question is 7 years old, I won't go to the effort to create a full explanation, but will link to this article from Yoast that explains why using relative URLs can be problematic.

1
  • 1
    Article is seven years old and concentrates on potential developer errors when using relative links - not technical reasons why relative links should not be used.
    – Zakalwe
    Aug 3, 2021 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.