Migrating your WordPress website to a new domain or server can be exciting, but it often comes with the headache of broken links. This happens because WordPress stores links as absolute URLs, including your old domain name. If these aren’t updated, your site’s navigation and content become a mess.
Don’t panic! Fixing these link errors is easier than you think. We’ll walk you through two proven methods to change all WordPress links to your new domain and get your site back in working order.
Why Do Links Break After a Domain/Server Migration?
WordPress stores links as absolute URLs, which include your old domain name. When you change your domain or server, these URLs become outdated and lead to 404 errors or redirect to incorrect pages.
Fix Broken Links in WordPress After Domain or Server Migration
There are two main methods to fix broken links in WordPress:
Step 1: Check the URL rewrite configuration of your server
Most websites use .htaccess
for Apache servers or specific configurations for Nginx servers. Ensure these are set up correctly. We have a separate article on how to Fix WordPress 404 Not Found Errors that you can refer to.
Step 2: Verify the correct domain name and URL structure.
If you’ve moved your website to a different server or changed the domain and can’t access the admin area, you can use SQL commands via phpMyAdmin or SSH if you’re using a Linux VPS.
1. Use MYSQL command to change URL
Log in to the server via SSH or open “Terminal” from your hosting panel/server, then log in to MySQL using the command below:
mysql -u user -p
use database_name;
In there:
- User: is the username that is managing the database of the current website, enter the password when asked.
- Use: is the command to select the database you want to perform, this is how to fix errors, edit URLs in wordpress that We often does.
Next, you copy the SQL command to change all WordPress links to a new domain and fix the old domain name and the new domain name for these statements:
UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');
Note:
- The old URL must be entered correctly including
http
orhttps
. - The new domain name too, you need to enter the correct Link/URL.
- “
wp_
” is your database prefix; you need to check and change if your database uses a different prefix.
After you have fixed the above statement, open MYSQL again and run the corrected statements to update the new URL for your site. Once done, clear your browser cache and check again that your website is working correctly with the URL.
Use SQL from phpMyAdmin to Change WordPress Domain Name
If you use hosting and have Cpanel, there is usually a database manager available with phpMyAdmin. Open phpMyAdmin, select the database you want to execute, then select SQL and run the WordPress link fix command in Step 2 as shown below.
In the image above, I changed the WordPress domain that failed from www to a URL without www. After successfully running the command, delete your browser history and visit your website again, now it has updated the URL to the new domain name.
Updating your WordPress links to a new domain is crucial for maintaining site functionality and SEO. By following these steps, you can ensure a smooth transition. If you found this guide helpful, please share it with others who might benefit!