-
-
- Submit Updated Articles to Bing and Yandex with IndexNow in Joomla
- Add Published and Modified Date Meta Tags to Joomla
- Add Open Graph and Twitter Card Tags to Joomla
- Auto-Generate Meta Descriptions from Article Text in Joomla
- Add Noindex to Specific Pages in Joomla
- Verify Your Joomla Site with Google Search Console
-
- Add a View as Markdown Link to Joomla Articles
- Display the Last Modified Date in Joomla Articles
- Embed Iframes in Joomla
- Automatically Link Twitter (X) @mentions in Joomla Articles
- Open External Links in a New Tab in Joomla
- Display Estimated Reading Time in Joomla Articles
- Replace Old URLs in Joomla
- Search and Replace in Joomla
-
- Add a Footer Line to the Joomla Administrator
- Show a Staging Banner in the Joomla Administrator
- Remove the "All" Option from Admin List Limits in Joomla
- Add a Cookie Consent Banner to Joomla
- Make the Administrator Navigation Sticky in Joomla
- Add Google Fonts to Joomla
- Add a Facebook Messenger Chat Button to Joomla
- Add a Reading Progress Bar to Joomla
- Add a Custom Scrollbar to Joomla
- Show Confetti on a Thank You Page in Joomla
- Add Smooth Scrolling to Joomla
- Add a Back to Top Button in Joomla
- Add a WhatsApp Chat Button in Joomla
-
- Protect the Administrator with a Secret URL Key in Joomla
- Show a Welcome Message on First Login in Joomla
- Log Out Idle Administrators in Joomla
- Assign New Users to a Different User Group Automatically in Joomla
- Redirect Visitors by Country in Joomla
- Redirect Users After Login by User Group in Joomla
- Block User Registration with Certain Usernames in Joomla
- Block User Registration by Country in Joomla
- Block User Registration by Email Address in Joomla
- Block User Registration by IP Address in Joomla
- Remove the Forgot Password and Username Links in Joomla
- Remove the Generator Meta Tag in Joomla
- Let Users Log In with Their Email Address in Joomla
- Block Traffic by Country in Joomla
- Block Bot Traffic in Joomla
- Block Traffic by IP Address in Joomla
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.
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.
How to add this snippet
- Install Code Snippets if it isn't installed already.
- Log into your Joomla backend.
- Go to Components → Tassos Code Snippets.
- Click New.
- Enter a title, for example Replace old URLs.
- Set Code Type to PHP.
- Set Insertion Method to Auto-Insert Locations.
- Set Location to On Page Load.
- Paste the code above into the editor.
- Replace the sample rules with your own.
- Click Save & Close.
Check that it works
- Open a page that links to an old address.
- 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.