Replace Old URLs in Joomla

Moved to a new domain or changed your URLs? Update every old link in your Joomla content at once, using one PHP snippet.

Replace Old URLs in Joomla
Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

You moved your site to a new domain or reorganized a section, and your redirects catch visitors who follow old links. The old addresses still sit inside your articles and modules, though, so every click costs an extra redirect, and some point to pages that no longer exist. You can update every old link on your site at once with one PHP snippet in Code Snippets.

The PHP snippet code

Right before Joomla sends a page to the browser, the snippet swaps old addresses for new ones in the finished HTML, in links and anywhere else they appear.

use Joomla\CMS\Factory;

// Edit your URL replacement rules here.
$replacements = [
    [
        'search'  => '#https://old-domain\\.com#',
        'replace' => 'https://new-domain.com',
    ],
    [
        'search'  => '#/old-section/#',
        'replace' => '/new-section/',
    ],
];

// Do not edit below.
$app = Factory::getApplication();

$app->registerEvent('onAfterRender', function () use ($app, $replacements)
{
    $document = $app->getDocument();

    // Skip non-HTML responses.
    if (!$document || $document->getType() !== 'html')
    {
        return;
    }

    $body = $app->getBody();

    if (!is_string($body) || $body === '')
    {
        return;
    }

    $newBody = $body;

    // Apply each URL replacement rule.
    foreach ($replacements as $rule)
    {
        if (empty($rule['search']))
        {
            continue;
        }

        $newBody = preg_replace($rule['search'], $rule['replace'] ?? '', $newBody);
    }

    // Update output only when something changed.
    if ($newBody !== $body)
    {
        $app->setBody($newBody);
    }
});

Each rule's search is a regular expression between # marks, with a backslash before each dot, as in the samples. The first rule moves every link from old-domain.com to new-domain.com. The second rewrites /old-section/ paths to /new-section/.

Your saved articles stay as they are, because the snippet only changes the page Joomla sends. RSS feeds keep the old addresses.

Keep your redirects in place. The snippet changes the links on your own pages, and visitors from search results and other sites still arrive at the old addresses.

How to add this snippet

  1. Install Code Snippets if it isn't installed already.
  2. Log into your Joomla backend.
  3. Go to Components → Tassos Code Snippets.
  4. Click New.
  5. Enter a title, for example Replace old URLs.
  6. Set Code Type to PHP.
  7. Set Insertion Method to Auto-Insert Locations.
  8. Set Location to On Page Load.
  9. Paste the code above into the editor.
  10. Replace the sample rules with your own.
  11. Click Save & Close.

Check that it works

  1. Open a page that links to an old address.
  2. Hover over the link, or view the page source.

The link should point to the new address.

To replace any text, not only addresses, see Search and Replace in Joomla. To learn more about how PHP snippets work, read the PHP snippet docs.

Last updated on Oct 15th 2026 14:10