Current Location: Home> Latest Articles> How to Use mb_eregi_replace to Replace URLs Across Your Entire Website, for Example, When Changing Domain Name

How to Use mb_eregi_replace to Replace URLs Across Your Entire Website, for Example, When Changing Domain Name

M66 2025-06-12

The Idea of Replacing URLs Across the Entire Website

Assume your website content is stored in a database or exists in text files, with URLs in the following format:

http://old-domain.com/path/to/page  
https://old-domain.com/another/page  

If you need to replace these with a new domain, for example, https://m66.net, you can:

  1. Define a regular expression to match the old domain URLs.

  2. Use mb_eregi_replace to replace them with the new domain URLs.

  3. Save the replaced content.

Example Code

<?php  
// Old domain (example)  
$oldDomain = 'old-domain.com';  
<p>// New domain<br>
$newDomain = 'm66.net';</p>
<p>// Content to process, assumed to contain multiple URLs<br>
$content = <<<EOD<br>
Welcome to our website: <a rel="noopener" target="_new" class="" href="http://old-domain.com/home">http://old-domain.com/home</a><br>
For more information, visit: <a rel="noopener" target="_new" class="" href="https://old-domain.com/about-us">https://old-domain.com/about-us</a><br>
Contact us: <a rel="noopener" target="_new" class="" href="http://old-domain.com/contact">http://old-domain.com/contact</a><br>
EOD;</p>
<p>// Define regular expression to match http or https followed by the old domain (case insensitive)<br>
$pattern = 'https?://'.$oldDomain;</p>
<p>// Use mb_eregi_replace to replace with the new domain<br>
// Replace with <a rel="noopener" target="_new" class="" href="https://m66.net">https://m66.net</a> + original path<br>
$replacedContent = mb_eregi_replace(<br>
$pattern,<br>
'https://'.$newDomain,<br>
$content<br>
);</p>
<p>// Output the replaced content<br>
echo $replacedContent;<br>
?><br>

Result:

Welcome to our website: https://m66.net/home  
For more information, visit: https://m66.net/about-us  
Contact us: https://m66.net/contact  

Advanced: Keep Path and Parameters Intact

In the above code, mb_eregi_replace only replaced the domain part and kept the path of the URL unchanged. This is because the regular expression matches http(s)://old-domain.com, and during replacement, only this part is replaced, leaving the following path intact.

If you want to match more complex URLs, you can write a more complete regular expression, for example:

$pattern = '(https?://)'.$oldDomain;  

In this case, mb_eregi_replace can still accurately match and replace the URL.


Summary

  • mb_eregi_replace supports case-insensitive replacement for multi-byte encodings, making it very suitable for processing text containing Chinese or other multi-byte characters.

  • By constructing the correct regular expression, you can easily perform bulk replacements of URLs in your website content.

  • Be careful to maintain the path and parameters of the URL when replacing to ensure that the links function correctly.

By using this method, you can quickly and efficiently replace all URLs across your website when changing domains, avoiding the inefficiency and risks of manually modifying each one.

  • Related Tags:

    URL