How to replace all hard-coded links in Confluence after the Base URL or AppLink URL is changed

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.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

When changing the Base URL or an application link URL,  either due to a URL change or from an export/import (e.g. Cloud to Server or Server to Server), links created as absolute links to the old URL (for example, using Insert > Link > Web Link) need to be manually 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/

Example Jira to Confluence 

Old Jira URLhttp://mycompany.com/jira
New Jira URLhttp://mycompanynewurl.com/jira


(info) this KB may be used in other types of URL changes, (warning) make sure you test this before on a test instance if using this procedure on cases not described here.

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

What about relative links?

If a link was created as a relative link (for example, by using Edit > Insert > Link, then from "Recently Viewed" or "Search"), it will change when the base URL changes. 

This document has more details: Working with Links.

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.

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

A PowerShell equivalent would be like this:

Powershell basic syntax example
Get-Content ./<export-file-name> | foreach{$_ -replace 'oldvalue','newvalue'}

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.

The examples below also place the output in a new entities_new.xml file. Be sure to use the entities.xml file name after all changes are done for the import to be successful.

Example Script with URL Values
sed -r -e 's/mycompany.com\/confluence/confluence.mycompany.com/g' entities.xml > entities_new.xml
Example Powershell script with URL values
Get-Content ./entities.xml | foreach{$_ -replace 'mycompany.com/confluence','confluence.mycompany.com'} | Out-File -path entities_new.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 example
    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 > entities_new.xml
    Powershell example
    Get-Content ./entities.xml | foreach{$_ -replace 'mycompany.atlassian.net/wiki/spaces/[A-Z0-9]+/pages/([0-9]+)/[A-Za-z0-9+.]+','confluence.mycompany.com/pages/viewpage.action?pageId=$1'} | Out-File -path entities_new.xml

    (info) In this example, the Powershell command will only work when using the single quotes ('), as double-quotes (") would break the ID replacement ($1).

  2. For CONFSERVER-45919 - Getting issue details... STATUS
    Replace incorrect Base URL entry from N/A

    sed example
    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 > entities_new.xml
    Powershell example
    Get-Content ./entities.xml | foreach{$_ -replace '<server.base.url>N/A</server.base.url>','<server.base.url>http://confluence.mycompany.com</server.base.url>'} | Out-File -path entities_new.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:

Starting from Confluence 7 (skip this step for older versions) we will need to set the flag on drafts to use content from pages when generating drafts instead of using Synchrony cache.
update CONTENTPROPERTIES set stringval='synchrony-recovery' where propertyname = 'sync-rev-source' and contentid in (select contentid from BODYCONTENT WHERE body LIKE '%old-URL%');
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%';

# Assuming that the HTTP protocol remains the same, specify the URL without a reference to the protocol. For example:
UPDATE LINKS 
SET destpagetitle = replace(destpagetitle,'//the.old-URL.com','//my.new-URL.com') 
WHERE destpagetitle LIKE '%//the.old-URL.com%';
UPDATE LINKS 
SET lowerdestpagetitle = replace(lowerdestpagetitle,'//the.old-url.com','//my.new-url.com') 
WHERE lowerdestpagetitle LIKE '%//the.old-url.com%';

# Make sure to set the URL in the last SQL query to lowercase
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 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 Mar 14, 2024

Was this helpful?

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