Remove Domain from Website Links in WordPress

WordPress does many magical things behind the scenes. HTML tags needed to display a blog post are added for us. Posts tagged with keywords are automatically connected to other posts with the same tag. Side bar widgets generate lists of recent posts, most used tags, etc. with no intervention from us. We just need to write the posts, add the tags, activate the widgets and WordPress does the rest. With all these automation features, I have overlooked a potential flaw in the system—they use absolute links.

Background

Okay, using absolute links isn't world ending. It may actually be a good thing for some cases. If we copy our database information into a WordPress install on a development server, for example, images with absolute links will work without needing to upload the files. The development server will pull them directly from the live website. My preference, however, is to utilize root-relative links.

Root-relatives links act pretty much the same as absolute links, but we're not locked to a specific domain name. That way the files still function when uploaded to a test server. Excluding the domain also simplifies the modifications necessary if the website's domain ever changes. WordPress, on the other hand, seems to be working against me. When using their upload tool for adding images or the feature for creating links, the full (or absolute) path is being used. There doesn't seem to be a way to prevent WordPress from using absolute paths, but at least it doesn't override the removal of the domain in links.

my impression was that links within posts were being managed. Things just always seemed to work when exporting database information from my live website and importing it into the development server. Images appeared as expected and links seemed to go to the desired page. While adding the Related Posts section to last week's blog entry, I noticed that the domain names were being included.

Changing the Links

When linking to content on our own website, there are a few different ways to create those links. As mentioned already, WordPress uses absolute links. So if we're linking to a graphic in the "images" folder which is housed in the websites root directory, the <img> tag might look like the following:

<img src="http://www.yourwebsite.com/images/yourimage.jpg" alt="">

Removing the domain portion turns the link into the root-relative version.

<img src="/images/yourimage.jpg" alt="">

Root-relative links are much cleaner and helps minimize the amount of work involved with switching to a new domain name.

Why Root Relative

One benefit of using WordPress, is that its fairly easy to pick up your data and move / copy it somewhere else. However, all those references to the domain name are still going to be included in the database.

So if we're looking to import the information into a WordPress install, some of the links will be pointing to the wrong domain name.

Conclusion

No automated option that I can see.

0 Comments

There are currently no comments.

Leave a Comment


Warning: Undefined variable $user_ID in /home/cyberscorp/webdev.cyberscorpion.com/wp-content/themes/scorpbytes/comments.php on line 72