Every now and then, an idea comes along that seems good, but when you think about it a little more, the flaws become apparent. This happened when I thought of moving my CDN to something other than Cloudflare, but then the disadvantages became suddenly apparent once I tried it. But there are other, more disruptive ideas that can explode in your face, and it’s best to know the advantages and disadvantages before jumping in. Relative URLs are one such example.
Why Consider Relative URLs?
A relative URL is one that doesn’t contain the domain name – just the part afterward. When a browser encounters such a URL, it assumes that it belongs to the same domain on which it’s hosted, and treats it as such. So instead of entering:
www.xyz.com/test
You would write instead:
/test
The first is an “absolute” URL, and the second is a “relative” URL. You can add the following code to your WordPress functions.php to ensure that the internal link selector inserts relative URLs, and not absolute ones:
function make_relative( $url, $post, $leavename ) {
if ( $post->post_type == 'post' ) {
$url = wp_make_link_relative($url);
}
return $url;
}
add_filter( 'post_link', 'make_relative', 10, 3 );
To learn how to insert code like this in WordPress, see this earlier tutorial on how to do it. Once you’ve added this code, you can bring up the internal link editor like this:
Select the post you want that’s already on your site, and it should enter a relative links as shown here:
So why would you want to do this?
Really the only reason I can see for this to make sense, is if you’re thinking of changing domain names one day and need to seamlessly transfer your site and for all URLs to work out of the box. But that reason seems very forced, since you can just do a “search and replace” directly in the database if necessary.
What About SEO? What Does Google Think of Internal Links?
As always before any change, we have to bow before the mighty Google and get their opinion. As of now, the official word is that Google doesn’t care if the links are relative or absolute as evidenced by this tweet:
But apart from this, there are some very good reasons NOT to use internal links, and stick to absolute ones instead.
1. Risk of Multiple Versions of Website
Website URLs can be written in a variety of ways:
example.com
www.example.com
https://example.com
https://www.example.com
etc…
Ideally, all versions of a website should redirect to ONE single canonical URL. Decide the format of your URLs, and then stick to it rigorously afterward.
But if your relative URLs redirect to another version of the URL, you could end up with multiple versions of the same content on different URLs, which could be disastrous in a number of ways. It’s not entirely clear how Google treats this kind of duplicate content, but it’s very bad form for a site and could have significant SEO issues, up to and including Google penalties.
2. Easier for Others to Steal your Content
With absolute URLs, a hacker has to put in more work to duplicate your website. They need to manually rewrite all your absolute URLs to point to their own site. Relative URLs on the other hand, make their work much easier. All they need to do is copy your site structure, and they’re done!
Web spammers are a lazy bunch. You’ll be surprised at how often simple precautions are enough to stop the majority of attacks!
Bottom Line: Absolute URLs are the Best
There’s no substitute for absolute URLs, and ones that all redirect to a single “perfect” URL. Here’s an earlier tutorial on the efficient way to redirect all incorrect URLs to one that has HTTPS and “www”. So if you’re toying with the idea of relative URLs, make sure you know exactly what you’re doing. Otherwise, the consequences could be severe!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
chris says
there is a tiny syntax error in your code snippet and the first dash (-) should be removed on that greater than sign so it looks like this:
function make_relative( $url, $post, $leavename ) {
if ( $post >= post_type == ‘post’ ) {
$url = wp_make_link_relative($url);
}
return $url;
}
add_filter( ‘post_link’, ‘make_relative’, 10, 3 );
Bhagwad Park says
HI Chris,
Thanks for the heads up!
Patrick says
Very bad article in my opinion.
1. Risk of Multiple Versions of Website: Absolute links only work with the exact URL, if you add HTTPS, change the domain and so on ALL links don’t work anymore.
– Different domain names (with/without www, http/https) should ALWAYS be dealt with by rewrite/redirect rules so that’s no issue
– If you’re planning to replace your website you have to use a different domain name for testing/creating purpose. After going live you would have to change all links. That’s just not practical as links can be in a lot of different locations/tables/plugins etc.
2. Easier for Others to Steal your Content: Stealing website content is barely an issue because if you really intent to do that you can easily just change all absolute links to relative links. Heard of search/replace?
What exactly would be the advantage of using absolute URLs, considering that changing an absolute URL to a relative is absolutely no issue?
Brian Bates says
Totally agree with you Patrick. Use of Absolute URLs in my view has a very limited use case, and just “feels wrong” from a professional IT perspective. You would be hard-coding stuff that really should not be hard-coded. It doesn’t matter how easily you can go in the back end, and search and replace… every change risks breaking something that you shouldn’t need to be touching, and ideally you should be changing it in a test environment, and then publishing to live rather than editing a live back-end directly. It might be ok as a hobbyist, but it doesn’t cut in if you are doing it professionally.
If I am going to build a website for somebody (as opposed to building “myfirstwebsite.com”, I may not even know what the final domain name is going to be when I’m building it, and that should not matter.
Finally, if I build a website for myclient.com and they also want to use myclient.net and myclient.co.uk and myclient.fr… what then? I want the same codebase to run everywhere seemlessly and ideally with as few complications (e.g. url-rewriting functions) as possible. I certainly don’t want to keep multiple copies of my codebase for every domain, if I don’t have to.
Wall.E says
There’s a least one good reason to use relative URLs: when you have a multi-language website with the language in the URL, like:
mywebsite.com/en/contact
mywebsite.com/fr/contact
If you link to the contact page from another page using relative a URL, you can just use ./contact.
Whether you are viewing the site in English or French doesn’t matter, it will work in any case.
But if you want to do it with absolute URLs, you’ll need each translation to have all its internal URLs personalized!
On an English page you’ll specifically need to setup: https://www.mywebsite.com/en/contact
While on a French page you’ll specifically need to setup: https://www.mywebsite.com/fr/contact
Etc.
Much heavier.
Bhagwad Park says
Wow, I didn’t even think of that use case. Good point!
Jim K says
And the same applies if you want to redirect mobile!
It was common to put a redirect to m.mysite.com from mysite.com or
mysite.com/m/.
There is no network admin with any talent anywhere that will tell you that hardcoding absolute URLs is a good idea.
I happen to be developing my new site with some fellow employees on the local LAN while my old site still exists. I want to move this site out to the internet from the LAN and I was astonished to learn that people actually think this is an OK practice.
None of the advice is this article is a best practice and is more of an example of what NOT to do.