Changing the domain for the WordPress service
This guide will help you change the domain and configure SSL certificates for your WordPress site. The following example is considered:
- The WordPress service is deployed on a server running AlmaLinux using the HTTPD (Apache) process and the SELinux mandatory access control system.
- This server is located in the
old-domain.comdomain and will be moved to the new domainnew-domain.com, registered by you yourself.
-
Edit the Apache virtual host configuration:
-
Open the Apache configuration file for your site. It is usually located in
/etc/httpd/conf.d/your-site.conf. -
Find the
VirtualHostblock and replace the current domain name in it with the new one.Example of a block with the new domain name:
<VirtualHost *:80>ServerName new-domain.comServerAlias www.new-domain.comDocumentRoot /var/www/html<Directory "/var/www/html">AllowOverride AllRequire all granted</Directory></VirtualHost>
-
-
If additional configuration files are used (for example, for subdomains), replace the domain name in them with the new one.
-
Restart Apache using the command:
sudo systemctl restart httpd -
If you have SELinux enabled, configure it to work with the new domain:
-
Restore the SELinux security context for the
/var/www/html/folder so that Apache has access to the new configurations. To do this, run the command:sudo restorecon -Rv /var/www/html/ -
Allow the HTTPD daemon to establish external network connections and access user home directories. To do this, run the commands:
sudo setsebool -P httpd_can_network_connect 1sudo setsebool -P httpd_enable_homedirs 1
-
-
Configure the SSL certificate.
-
Install the
Certbotpackage for automatic obtaining of certificates from Let's Encrypt by running the command:sudo dnf install certbot python3-certbot-apache -
Run
Certbotand follow the instructions to automatically install the SSL certificate on the new domain:sudo certbot --apache -d new-domain.com -d www.new-domain.com -
When
Certbotoffers HTTP to HTTPS redirection, enable this option. This will allow automatically redirecting users to a secure connection. -
After the SSL certificate is installed, enable its automatic verification and renewal by running the command:
sudo systemctl enable certbot-renew.timer -
Restart Apache using the command:
sudo systemctl restart httpd
-
-
Change the URLs in the WordPress settings:
-
Navigate to
http://new-domain.com/wp-adminin your browser. -
Log in to WordPress using the administrator username and password.
-
Go to the Settings → General section.
-
Replace the domain name with the new one in the WordPress Address (URL) and Site Address (URL) fields:
https://new-domain.com -
Click the Save Changes button.
-
-
Replace all links to the old domain with new ones in the content and database settings by running the commands:
cd /var/www/htmlwp search-replace 'http://old-domain.com' 'https://new-domain.com' --allow-root -
If you use a caching plugin, for example, WP Super Cache or W3 Total Cache, clear the entire cache via the plugin settings.
-
Regenerate the permanent URL structure on the site. To do this, go to the Settings → Permalinks section and click the Save Changes button.
-
Check the SSL certificate:
- Go to the site and make sure it opens via the HTTPS protocol without errors.
- Use online tools, such as SSL Labs, to verify the correctness of the SSL certificate.
-
Make sure that all requests to the old domain are correctly redirected to the new one. This can be done using the Redirect Checker tool.
-
Check the site functionality:
- Browse the pages of the site and make sure that the links work correctly.
- Make sure that all media files are loaded via the new URL links.
To improve security, configure HSTS — a mechanism that forcibly activates a secure connection via the HTTPS protocol. To do this, add the following lines to the Apache configuration:
<VirtualHost *:443>Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"</VirtualHost>
- If your site has already been indexed by search engines, submit a new XML sitemap to Google Search Console and Yandex Webmaster, specifying the new domain name in it.
- If you actively maintain social media or other external resources, update the links to your domain in them.