How to replace all hard coded links after a base URL change

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform Notice: Server and Data Center Only - This article only applies to Atlassian products on the server and data center platforms.

Summary

When changing the Base URL, either due to a URL change or from an export/import (e.g. Cloud to Server or Server to Server) absolute links to the old Base URL need to be changed.

Example Server to Server

Old Base URLhttp://mycompany.com/confluence
New Base URLhttp://confluence.mycompany.com/

Example Cloud to Server (this requires extra steps)

Old Base URLhttp://mycompany.atlassian.net/wiki
New Base URLhttp://confluence.mycompany.com/

Please note that Cloud to Server Base URL change requires extra steps due to these known issues

Environment

Confluence Server or Data Center

Solution

Always back up your data before performing any modifications to the database. If possible, test any alter, insert, update, or delete SQL commands on a staging server first.

Option 1: Export/Import Option using Find and Replace

This option outlines how to export your Confluence data and perform a find/replace leveraging the sed script notated below to update the link values. 

1. Choose an export method and generate the export file

Option A: Generate a site XML export file

(warning) The XML backup method can be a resource-intensive operation that can cause performance issues depending on the size of your instance. If your Confluence instance is very large, we would recommend running this operation over a maintenance window in an abundance of caution.

Full site export

  1. Go to > General Configuration > Backup & Restore
  2. Check Back up attachments
  3. Click Back Up
  4. Find the XML export file (usually) in <confluence-home>/temp
  5. Extract the entities.xml file from the XML export file
  6. Shut down Confluence

Option B: Generate a database export file

Use a native database tool to export all the data. This requires Confluence to be shut down.

    • For MySQL use mysqldump

    • For PostgreSQL use pg_dump

2. Run the sed script against your export file to find and replace the old URL value with the new value

Run the following sed script on the export file you generated.

Basic Syntax Example
sed -r -e 's/oldvalue/newvalue/g' <export-file-name>

As an example, the following script would be running for an instance where the old URL is http://mycompany.com/confluence and the new URL is http://confluence.mycompany.com. Note that the script contains escapes for the forward-slash characters contained in the URL. This example references an example site export file entities.xml.If you are running this against a database export, you would replace this file with your resulting export.sql file.

Example Script with URL Values
sed -r -e 's/mycompany.com\/confluence/confluence.mycompany.com/g' entities.xml
For Cloud exports only ...

Cloud exports will use the entities.xml file for the backup zip

  1. For CONFSERVER-54759 - Getting issue details... STATUS
    Replace all content URLs to Confluence Server format with this command (using the example URLs above)

    sed -r -e 's/mycompany\.atlassian\.net\/wiki\/spaces\/[A-Z0-9]+\/pages\/([0-9]+)\/[A-Za-z0-9+.]+/confluence.mycompany.com\/pages\/viewpage.action?pageId=\1/g' entities.xml
  2. For CONFSERVER-45919 - Getting issue details... STATUS
    Replace incorrect Base URL entry from N/A

    sed -r -e 's/<server\.base\.url>N\/A</server\.base\.url>/<server.base.url>http://confluence.mycompany.com</server.base.url>/g' entities.xml

3. Reimport the updated export file

Option A: Site XML Import

Using this option will require extra steps to re-load all the Add-ons from http://marketplace.atlassian.com/

  1. Re-zip up the XML export file, taking care to place the files in the same location as the original export.
  2. Go to > General Configuration > Backup & Restore
  3. Click Choose file and select the new XML backup zip file
  4. Click Upload and Restore
Option B: Database import

Use the database tools to re-import the data.

Option 2: WebDAV option

Use the WebDAV plugin and mount the WebDAV location locally, then run a script that uses sed to replace all of the Old Base URLs. This option creates a new page version for all pages that are changed (this includes any page versions that are crawled and edited).

Option 3: Confluence Source Editor option

You can use the Confluence Source Editor app to modify content on a page-by-page basis.

  1. Install the Confluence Source Editor app.
  2. Open the editor on an affected page.
  3. Click the <> button on the top right of the editor.
  4. Replace any occurrences of the old base URL with the new base URL.
  5. Save the page.

This technique has the benefit of being less risky than doing direct data manipulation and it's easier than manually linking the attachment through the editor interface. 

Option 4: Database query

Confluence stores page content in plain text, on the 'BODYCONTENT' table. It's also possible to replace these links by replacing text on this table, with the following queries:

Replacing the links for all page versions (Compatible with PostgreSQL, Oracle, MySQL)
UPDATE BODYCONTENT 
SET body = replace(body,'old-URL','new-URL') 
WHERE body LIKE '%old-URL%';
Replacing the links for all page versions (Compatible with SQL Server 2017 - Microsoft Docs - ntext, text, and image (Transact-SQL))
UPDATE BODYCONTENT
   SET BODY = REPLACE (CONVERT(VARCHAR(MAX), BODY), 'old-URL','new-URL')
 WHERE BODY LIKE '%old-URL%';
Extra: Replacing the links for the latest versions of pages from active spaces (Compatible with PostgreSQL)
UPDATE BODYCONTENT
SET BODY = REPLACE (BODY,'old-URL','new-URL')
WHERE BODYCONTENTID IN (SELECT bc.BODYCONTENTID
                        FROM BODYCONTENT AS bc
                        INNER JOIN CONTENT c ON c.CONTENTID = bc.CONTENTID
                        INNER JOIN SPACES s ON s.SPACEID = c.SPACEID
                        WHERE bc.BODY LIKE '%old-URL%'
                          AND c.PREVVER is NULL
                          AND c.CONTENTTYPE ='PAGE'
                          AND c.CONTENT_STATUS = 'current'
                          AND s.SPACESTATUS = 'CURRENT');

This will replace all instances of 'old-URL' on pages, with the text from 'new-URL', automatically replacing the links on Confluence pages.

  • (info) Restart Confluence or flush the cache in General Configuration → Cache Management to have these changes be reflected in the UI.

(warning) Try this on a test Confluence instance beforehand, and backup the Confluence database first




Last modified on Oct 20, 2020

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.