WordPress.org

Ready to get started?Download WordPress

Forums

Using relative instead of absolute links for images? (18 posts)

  1. jderosa3
    Member
    Posted 6 years ago #

    I am starting to really like wordpress. It is quite easy to use but I still get hiccups every once in a while.

    When I make a post I may have images I want to use. I store these images in:

    http://www.mydomain.com/images

    when I would make a post I would use the following code to insert an image.

    <img src="images/image.jpg"/>

    but... the image would not show or give that paper icon with an "x" to show that the file could not be found.

    So to get it to work I would have to use this:

    <img src="http://www.mydomain.com/images/image.jpg"/>

    I prefer not to code this way, but I am not sure how to make this work.. .Any ideas? is this because I should be using the "images" directory within my theme folder?

    Thanks for any help.

  2. whooami
    Member
    Posted 6 years ago #

    <img src="/images/image.jpg"/>

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    I prefer not to code this way

    Out of curiosity, why?

    Use of absolute URIs is the generally preferred approach. Relative URIs is bad practice and bad form, generally speaking. It might be fine for a static site, or one that is intended to be moved to a new location, but for a dynamically generated site which has a permanent URL/domain, absolute URIs just make more sense.

  4. isho
    Member
    Posted 6 years ago #

    I disagree. Site relative urls (eg. /images/image.jpg ) are generally speaking the best practice for dynamically generated sites.

    Absolute urls can be such a headache especially when a site needs to be accessible from multiple domain names. On an internal vs. external network for example, or a lot of people like their site to be accessible from both mydomain.com and http://www.mydomain.com. Or what about a site accessible via https and http?

    But more importantly it makes the site really difficult to move from one domain to another. This is really common for professionally built sites that involve continuous code development. You'll have development servers, a stagging server and the live server. Granted you'll hardly ever move data between dev and production, but it's not that uncommon to move data between dev -> stage and also stage -> live.

    I'm currently working on my first real wordpress customization site and the absolute url thing is proving to be a pain. From reading through the forum it doesn't sound like there are a lot of great solutions out there, someone please let me know if I'm wrong. I made an attempt to go through the codebase and change the way urls are generated where ever I found it generating absolute urls where I wanted site relative ones. It seemed to work pretty well but I'm hesitant to use these changes because I'm afraid it will break stuff that actually needs an absolute url (eg. in the RSS feeds)

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Absolute urls can be such a headache especially when a site needs to be accessible from multiple domain names.

    A site should never, ever be accessible at two URIs. The one URI should redirect to the other one. This not only provides a canonical location, but it is a lot more search engine friendly, as having the same content under more than one URI will incur a duplicated content penalty.

    On an internal vs. external network

    The two networks should be configured properly to map the domain name correctly on both sides of the network. If it needs to use an internal IP for that domain on the inside, then the internal DNS server should be configured appropriately.

    or a lot of people like their site to be accessible from both mydomain.com and http://www.mydomain.com. Or what about a site accessible via https and http?

    Both of these cases should use 301 redirects to redirect one of them to the other of them. www to non-www, for example. In the specific case of https, the server should only use https for the pages which need to be secured, as use of SSL encryption incurs an overhead which should be managed to be as low as possible.

    But more importantly it makes the site really difficult to move from one domain to another.

    a) It's really not that difficult. Using a simple search and replace tool which can handle multiple files is rather trivial. And your database backup is just a text file, search and replace works fine on it too.
    b) You really should never change domains. I mean, not after the initial phase. All you're doing by changing domains is making it harder for your audience to find you after the move. Your domain is your identity, why would you shift it to somewhere else once it has become established?

    This is really common for professionally built sites that involve continuous code development. You'll have development servers, a stagging server and the live server. Granted you'll hardly ever move data between dev and production, but it's not that uncommon to move data between dev -> stage and also stage -> live.

    As a person who writes just such servers (my latest one handled $1.5 billion in transactions last year), I can say with authority that in no circumstance would data ever, ever, *ever*, move from a dev box to the production system. That would be just cause for getting fired. Period.

    The other way (production to dev) would be acceptable. You have to test problems in dev, and sometimes production data is the only way to do it. But the dev data stays in dev. Nothing, and I mean nothing, moves into production without rigorous testing and no data ever moves in that direction.

    I'm currently working on my first real wordpress customization site and the absolute url thing is proving to be a pain.

    In what specific way? I've always found relative URLs to be a massive pain, because they cannot be trusted. The actual relative URI of an item will change extremely often on a dynamic site, because the URI is not linked to a specific directory structure.

    A URI is an item's absolute location. It should never, ever change. Or, at least, changing it should be a big deal, to be considered and planned for in advance. A relative URI however, is relative to the current URI. What relative URI do I use for an image when my main page is http://example.com and my single page is http://example.com/2008/03/03/post-name and my category archive page is http://example.com/category/cat-name/ and all three of these can contain the same content? Sure, I can use a root-relative path, but using the absolute URI makes way more sense.

  6. jderosa3
    Member
    Posted 6 years ago #

    Otto42 Amazing write up! Everyone should read this post explains everything anyone would want to know about absolute and relative URls - Thank you for this amazing job - it helped out a lot!

  7. binarymoon
    Member
    Posted 6 years ago #

    Ignoring all the other arguments I would say absolute urls are essential for one reason.

    Feed Readers

    Any images with relative urls will not work in feed readers since they will try to load the image relative to the reader (eg load from reader.google.com) which clearly won't work.

  8. PierreFWalter
    Member
    Posted 5 years ago #

    Okay, then, absolute links, but why does WordPress not intelligently adapt them to the server URL? Example. I have MAMP (mamp.info) and thus could install WordPress on my Mac Pro, and an image URL would look like that:

    http://localhost:8888/wordpress/wp-content/uploads/2008/06/blue_eye.jpg

    Then I uploaded to my site and of course, the images were not to be seen because they were coded absolutely. I tried to code them as relative, but WordPress didn't allow me to.

    So I had to change all images from their initial path
    http://localhost:8888/wordpress/wp-content/uploads/2008/06/

    to

    http://mydomain.com/wp-content/uploads/2008/06/

    I want to see the designer who will go and change the URL for each and every image in a huge web.

    So the solution is to setup a directory /images on the server and link to the images via URL. This also goes around just another random bug in WordPress which is to upload the images when you upload the site via xml import, but to equip each image with a question mark - impossible to use it. Why - only god knows.

    Needless to add that such a way of doing is even more intelligent when we talk about media files. It's easy to upload them to a /media or whatever directory on the server using FTP and then linking to the media. This also allows later to put a copy of the site, if so wanted, on wordpress.com bypassing their limitation to upload media files other than PDFs without upgrading.

    If discussions could be less ideological, would be really nice. I don't think it's a crime to put relative links in a site. We all have our preferences:)

  9. NC@WP
    Member
    Posted 5 years ago #

    PierreFWalter,

    I want to see the designer who will go and change the URL for each and every image in a huge web.

    Why not? In a database-driven site, this is a one-query job...

  10. zabadac
    Member
    Posted 5 years ago #

    The original question has been poorly answered in my opinion.

    WordPress either supports the use of relative URLs, or it doesn't.
    I agree with 'jderosa3 that for porting sites it would be useful, when you generally need to prepare the site before switching DNS setting. Th same is true for development, prior to deployment in a live environment.

    Such sites in preparation do not need to point to another server location defined by live DNS settings. It makes the process of testing the site being ported difficult and incomplete, and forces a 'live' test before you can besure everything has been done properly. This is irregular practice. Indeed it IS good practice to 'wrap' a site as a self contained entity, for reliable running, ease of updates and maintenance.

    If relative links are not supported, then a solution is, as suggested, to simply host the images elsewhere and use the absolute links. However, this is not always ideal, it's a compromise.

    The Feed Reader argument is not a case against the support for relative links, just a case that the Feed Reader situation needs to be looked at.

    best regards, Peter

  11. zabadac
    Member
    Posted 5 years ago #

    You may find this discussion (written a few years ago) about relative links in WordPress interesting: For the original question, we are talking about 'absolute relative' links, where really only the domain name is ommitted from the link.

    http://mathiasbynens.be/archive/2005/02/relative-links

    It includes a plugin that works well:
    http://plugins.trac.wordpress.org/browser/relative-links/

    I believe it may not necessarily with other plugins although it can be made to work.

    Also, there is a possible performance issue. I've tried it on some 'post and page' images, and I think for a modest WP site the performance question is not an issue.

    A live example is on http://avondaleaudio.com
    I placed the images in a directory called 'webimages' under the main public_html directory for the website, not for any technical reason, just preference.
    In the post/page, the link is simply:
    /webimages/imagefilename.extension

  12. anax
    Member
    Posted 5 years ago #

    Glad I found this thread because all the documentation and various tutorials I read about moving blogs didn't mention this issue. I had to move a blog to a local server (that was for display and didn't have an internet connection).

    I eventually found the SQL query I needed to run was
    UPDATE wp_posts set post_content=REPLACE(post_content, 'www.mydomain.co.uk/', 'localhost/mylocaldomain_directory/')

    Hope that might help others.

  13. l r
    Member
    Posted 5 years ago #

    Yes good thread.. and I feel I need to throw in my pennysworth.
    I move from dev to live often eg from http://www.domain/foldername to http://www.domain.

    It would be good if relative/absolute was a user choice. It's a personal preference.

    Most designers and css coders should not have to worry about writing sql queries to fix image paths. Not everyone is an sql whizz and that is the pure joy and the advantage of wordpress.

    Domain addresses do change. For development reasons and for branding redesign, company name changes etc. In an ideal world it shouldn't but I have come across it many times. In fact it's a daily occurance as a front end designer/builder.

    I build entire websites in dev areas and upload to domain root. If I use relative url's it is almost always a smooth transfer process where-as Absolute url's break and need fixed.

    Absolute url's are not my preference (though I wholeheartedly agree with the above points about not changing url and not having two urls on one domain)

    Absolute urls with wordpress presumes you can use sql to search and replace. A daunting task to those that are not familiar.

    In saying all of this can anyone advice on getting relative image paths in media upload and relative text links in the editor for 2.7???

    I am personally gutted I can't seem to easily choose.(or can I?)
    I am wary of using my preferred method if relative url's are not equally supported in wordpress release though..

  14. iridiax
    Member
    Posted 5 years ago #

    If you really want to use relative links or image paths, then don't use WordPress (or anything similar). Due to the nature of WordPress, a post's content can appear in multiple locations (post, edit post page, home page, category archive, tag archive, date archives, search result page, feed), each with a different URL, making relative links useless.

  15. l r
    Member
    Posted 5 years ago #

    Am i missing something really obvious - that is very different form other cms systems?
    I have used relative urls successfully in other cms systems.. that's with tagging and multiple locations.
    When the site root changes you call a dynamic root url and everything still works.
    EG http://www.mydomain.com/2008/10/
    changes to http://www.mydomain2.com/2008/10/

    If i have 2008/10/ as my relative url in content..and the root is dynamic and called automatically as a base what can go wrong..
    All other path parts are unchanged. Am i making any sense?

  16. Saurus
    Member
    Posted 5 years ago #

    So .... for those who may not be a fluent as Otto and iridiax on this subject ...

    An absolute link would include the full path to the image, as in "http://URL/images/image name", while a relative link would use only "/image name" ?

    Rather confusing for us old folks whose brains don't function too well all the time :)

    Ha! And practicing what I usually preach, I found a rather simplified explanation HERE: http://www.extropia.com/tutorials/web_design/relative_absolute_links.html

  17. l r
    Member
    Posted 5 years ago #

    It's understood that..
    relative link = /images/image name and
    absolute = http://URL/images/image name

    But with wordpress why is there no 'dynamic root url' option.
    In other cms systems (eg drupal and cmsmadesimple) you can choose to add relative or absolute links within the wysiwyg editor.
    This difference is when you choose relative links.. a root url is set in config just once.
    The base href was called to the front end via eg base=true or $base_url.

    The root url is set in one place only... and is easy to change.

    If you choose relative settings then the page content editor never hardcodes the full url path into content pages.

    So content looks like this in the editor pages /images/imagename.

    However

    When you view source on the front end - the root url - declared just once in the config is used as a base and is passed to each path. This is dynamic and it is not hardcoded in individual pages. It is called from one root setting in config.

    So you have http://www.mydomain.com/images/imagename
    or http://www.mydomain2.com /images/imagename
    on front end source
    and
    /images/imagename.
    in the page editor source.

    So when you want to change your root - you only change it once.
    In the config file.
    Other systems do deal with the root url change via scripting.

  18. l r
    Member
    Posted 5 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic