---
title: "Replace Old URLs in Joomla - Tassos Code Snippets"
description: "There are cases where you would like to update outdated links on your site. Most administrators can use a redirection component to redirect old URLs t"
url: "https://www.tassos.gr/docs/tassos-code-snippets/howto/content-enhancement/replace-old-urls-joomla"
date: "2026-04-05T04:08:12+00:00"
language: "en-GB"
---

[ Home ](https://www.tassos.gr/index.php?option=com_content&view=category&layout=blog&id=24&Itemid=1088) / [ Tassos Code Snippets ](https://www.tassos.gr/index.php?option=com_content&view=category&id=120) / [ How-to Guides ](https://www.tassos.gr/index.php?option=com_content&view=category&id=125) / [ Content Enhancement ](https://www.tassos.gr/index.php?option=com_content&view=category&id=142)

#  Replace Old URLs in Joomla

There are cases where you would like to update outdated links on your site. Most administrators can use a redirection component to redirect old URLs to new ones, but old URLs may still remain inside the content itself. This is a common task in Joomla: replacing old URLs when migrating domains, moving folders, or switching from old paths to new ones. Hopefully, you don't need an extra plugin. You can do it with Tassos Code Snippets using a simple PHP snippet.

## [The PHP Snippet Code](#the-php-snippet-code)

This code lets you define URL replacement rules in one place and apply them to your site's final HTML output. It supports both simple text replacements and regex-based replacements, so you can handle full URL swaps and pattern-based URL updates.

 ```
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);
    }
});
```

## [How to add this snippet](#how-to-add-this-snippet)

1. Install [Tassos Code Snippets](https://www.tassos.gr/docs/tassos-code-snippets/getting-started/how-to-install) if it is not installed already.
2. Go to your Joomla Administrator area.
3. Open **Components** → **Tassos Code Snippets**.
4. Click **New**.
5. Select **PHP** as the snippet type.
6. Select **Insertion Method** → **Page Load**.
7. Paste the code above to replace old URLs.
8. Optionally, open the **Conditional Logic** tab to restrict which pages or users the snippet runs on. Leave it empty to run it site-wide. Learn more in [Using Snippet Logic](https://www.tassos.gr/functionality/using-snippet-logic.md).
9. Publish the snippet.

Congratulations! Now, your Joomla website can dynamically replace old URLs with new ones without installing another plugin or adding unnecessary bloat. To learn more, visit the [PHP Snippet](https://www.tassos.gr/docs/tassos-code-snippets/types/php) works

## [Use Cases](#use-cases)

- Replace old domain URLs after a site migration
- Update hardcoded links from HTTP to HTTPS
- Replace deprecated section paths with new ones
- Fix outdated documentation links in Joomla content
- Keep internal links aligned with the current site structure

 Last updated on Mar 19th 2026 13:03

## Schema

```json
{
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement": [
        {
            "@type": "ListItem",
            "position": 1,
            "name": "Home",
            "item": "https://www.tassos.gr"
        },
        {
            "@type": "ListItem",
            "position": 2,
            "name": "Home",
            "item": "https://www.tassos.gr/docs"
        },
        {
            "@type": "ListItem",
            "position": 3,
            "name": "Tassos Code Snippets",
            "item": "https://www.tassos.gr/docs/tassos-code-snippets"
        },
        {
            "@type": "ListItem",
            "position": 4,
            "name": "How-to Guides",
            "item": "https://www.tassos.gr/docs/tassos-code-snippets/howto"
        },
        {
            "@type": "ListItem",
            "position": 5,
            "name": "Content Enhancement",
            "item": "https://www.tassos.gr/docs/tassos-code-snippets/howto/content-enhancement"
        },
        {
            "@type": "ListItem",
            "position": 6,
            "name": "Replace Old URLs in Joomla",
            "item": "https://www.tassos.gr/docs/tassos-code-snippets/howto/content-enhancement/replace-old-urls-joomla"
        }
    ]
}
```

```json
{
    "@context": "https://schema.org",
    "@type": "Article",
    "mainEntityOfPage": {
        "@type": "WebPage",
        "@id": "https://www.tassos.gr/docs/tassos-code-snippets/howto/content-enhancement/replace-old-urls-joomla"
    },
    "headline": "Replace Old URLs in Joomla",
    "image": {
        "@type": "ImageObject",
        "url": "https://www.tassos.gr/"
    },
    "publisher": {
        "@type": "Organization",
        "name": "Tassos",
        "logo": {
            "@type": "ImageObject",
            "url": "https://www.tassos.gr/https://www.tassos.gr/media/brand/logo-text.png"
        }
    },
    "author": {
        "@type": "Person",
        "name": "Tassos Marinos",
        "url": "https://www.tassos.gr/docs/tassos-code-snippets/howto/content-enhancement/replace-old-urls-joomla"
    },
    "datePublished": "2026-03-09T15:34:47+02:00",
    "dateCreated": "2026-03-09T15:34:47+02:00",
    "dateModified": "2026-03-19T13:58:11+02:00"
}
```
