WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Force ONLY relative links (16 posts)

  1. deanes02
    Member
    Posted 5 years ago #

    Hi, I develop a lot of sites as a sub domain of my domain initially and then when the site is ready to go live I change all the WP blog settings domain settings etc to my clients web address.

    For example, http://myclient.mydomain.com for development becomes http://www.myclient.com when it goes live.

    So the problem is that WP uses absolute links when you create a post and when you add an image through the media library (and probably other places too). Therefore, when the site goes live there are still references to http://myclient.mydomain.com in the WP database. I am not talking about the settings in the Admin > Settings page.

    Is there any setting to tell WordPress to ALWAYS use relative links?

  2. Shane G
    Member
    Posted 5 years ago #

    Hi,

    As you have developed blog within the site and now want to move it to the root directory of the domain then you need to make some changes in the settings and database which are listed in this article...refer them:

    http://codex.wordpress.org/Moving_WordPress

    Once you follow them...you can successfully move your blog to the root of your domain..

    Thanks,

    Shane G.

  3. deanes02
    Member
    Posted 5 years ago #

    Thanks. That was the process I was using - pretty much. Its easy enough to export the database, do a find and replace for the web addresses and import the database again.

    However I was kind of hoping the good people at wordpress would have a little tickbox somewhere, that said, "only use relative links"....

    If they do, or intend to please let me know. If they don't then I wonder why not?

  4. iridiax
    Member
    Posted 5 years ago #

    Relative links do not work with WordPress because the same content can display under a variety of different URLs. A post and its images can be on a home page, single post page, category archive, rss feed in a feed reader, and so forth, each with a different URL structure.

  5. ryans149
    Member
    Posted 5 years ago #

    I think you are talking about the media links in posts, like image links.

    These links are saved as a part of posts in your database. You can not use a relative link (or say physical link) in your database, as it will not have any reference.

    As a solution, you can use an SQL query and replace :

    myclient.mydomain.com

    with

    myclient.com

  6. deanes02
    Member
    Posted 5 years ago #

    At least I now understand why they don't have the relative link option and thank you for that - it makes perfect sense.

    Is there an SQL query that does a find and replace across the whole database? The following code does it on a table by table basis ---
    update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');

    Unless there is an SQL query to find and replace for the whole database, I would think that Exporting the whole database (using phpMyAdmin) then doing a find and replace in your text editor (I use PSpad) and importing the database again, seems like a quicker way.

    Any thoughts?

  7. ryans149
    Member
    Posted 5 years ago #

    You are welcome!

    yes, this is a good idea.

    I am not sure how to do this, as I am not good in SQL. But you can download the database on your local and do experiments..

  8. jberghem
    Member
    Posted 4 years ago #

    Relative links do not work with WordPress because the same content can display under a variety of different URLs. A post and its images can be on a home page, single post page, category archive, rss feed in a feed reader, and so forth, each with a different URL structure.

    This is not the case if the links are relative to the root. Therefore there should be a way to do what deanes02 asked. I also have the same problem because the import export option changes category ID's and if you have a theme with custom category templates for specific categories (i.e. category-4.php) you have to edit and/or rename those template files. Which I personally find very annoying.

  9. sergeletko
    Member
    Posted 4 years ago #

    hi,
    just wondering if it will work to move WP installed locally to a live online host...

    for the MySQL querry check out this post : http://www.mydigitallife.info/2007/10/01/how-to-move-wordpress-blog-to-new-domain-or-location/

    hope that will help...

    if anyone has a solution to move from local to live hosting, I'm interested : http://www.monmillion.fr

  10. motoroilinfo.com
    Member
    Posted 4 years ago #

    I use relative links on my site here: http://syntheticoilarticles.us. They all work fine. Let's say the link to an image is: http://syntheticoilarticles.us/wp-content/uploads/2009/10/racing_boat.jpg. I replace it with: /wp-content/uploads/2009/10/racing_boat.jpg and everything works out just fine. The browser assumes the link is originating from the root directory (where the links actually are, remember they are being called to the index.php file in the root directory and will automatically put in the website url before hand. Let us assume you make a link to a sitemap on your website. It can be handled one of three ways:
    Sitemap, or
    /sitemap/>Sitemap
    or the relative link: Sitemap

    All three will work just fine.

  11. motoroilinfo.com
    Member
    Posted 4 years ago #

    Let me correct the above codes where they will show up properly. I am omitting the tags.

    URL 1: href="http://www.yoursite.com/sitemap/"
    URL 2: href="http://<?php bloginfo('url'); ?>/sitemap/"
    URL 3: href="/sitemap/"

    All three work just fine.

  12. dotgain
    Member
    Posted 4 years ago #

    Look at the page source for any WP-generated page: all A HREF and IMG SRC and SCRIPT SRC tags are generated using absolute links. Even when it's to the same pathname on the same server hostname.

    So if I have a WP server and the hostname is thisismywordpress.testing.example.com and I put some content under /testing/project/ there is a potential extra chunk of text (http://thisismywordpress.testing.example.com/testing/project/) on every link, adding to the page size (more work for the server, more data transfer, more work for each and every client, more time wasted by every visitor).

    If you are thinking "oh, it's only a few bytes", please skip to the the next thread: it all adds up, and for someone on a low-RAM device (let's say a mobile phone) it could mean the difference between a usable site and a source of "out of memory" errors.

    Is there any way to make WP use relative links where it can for all generated HTML output? Even something as simple as removing the hostname where it is on the same host would make moving from one server to another more painless.

    I understand that for images and templates etc, stuff has to go under /wp-content/ or other fixed directories, so fully-relative links (using ../../foo/) may not work. But they don't necessarily work for static HTML websites either.

    As for moving blogs or pages (or whatever other content, be it WP-generated or static and uploaded), if my testig project will end up on http://thisismywordpress.com/project/ that is 2 move types, moving hostname and moving directory. And, as the testing may be in phases (some private-access, some public-access), those moves may happen at different times or in a different order.

    For moving content around: as jberghem said, it should still be possible to do what deanes02 wants to do, using links that are relative to the root.

  13. rgbk
    Member
    Posted 4 years ago #

    Did amyone make any headway on this matter?

    Surely wordpress should have fixed this by now?!

    Seems like such a hacky, inelegant way to transfer!

    Thanks,
    Marc

  14. valarcher
    Member
    Posted 4 years ago #

    I use this solution for relative links, just a few lines added to functions.php:

    http://designintheraw.com/2010/tutorials/wordpress-relative-links-post-shortcode-api/

    Val

  15. halles
    Member
    Posted 4 years ago #

    I created another solution; a plugin that will strip the protocol and hostname from the url, leaving just a realtive url. It won't strip the stuff from links already inserted into posts or pages but new links will just be created like said. You can download the stuff from here: http://halles.cl/wp/relative-links.php.zip or http://halles.cl/wp/relative-links.php.txt

    I made it a plugin because it didn't make a lot of sense putting into a single template, changing the template would break the system.

    Hope you find it useful1!

  16. sharris203
    Member
    Posted 4 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic