I recently changed the domain for my personal blog. After the switch, I needed to set up 301 redirects from the old domain to the new one — for example, visiting https://zlbk.net (old domain) should automatically redirect to https://imzl.com (new domain), seamlessly.
This not only improves the visitor experience but also transfers domain authority and search engine indexing. This article documents the methods for implementing 301 redirects.
Using WordPress to Implement URL-Specific 301 Redirects
- Please note: this method requires keeping the original WordPress files on the old domain. If you’ve already run a SQL find-and-replace on the domain in the original codebase and the old domain no longer has a separate WordPress installation, this method won’t work.
- Locate the
wp-blog-header.phpfile in your WordPress root directory and add the following code below the first<?phpline:
// WordPress domain change 301 redirect
// Replace imzl.com with your new domain
if (strtolower($_SERVER['SERVER_NAME'])!='imzl.com')
{
$URIRedirect=$_SERVER['REQUEST_URI'];
if(strtolower($URIRedirect)=="/index.php")
{
$URIRedirect="/";
}
header('HTTP/1.1 301 Moved Permanently');
header('Location:https://imzl.com'.$URIRedirect);
exit();
}
- Replace the corresponding file on your server. Done.
Using Nginx to Implement URL-Specific 301 Redirects
This method does not require keeping the WordPress codebase.
- Locate your Nginx configuration file and add the following code below the
server_namedirective:
// Replace imzl.com with your new site's domain
return 301 https://imzl.com$request_uri;
- Upload the modified Nginx configuration file and restart Nginx via terminal using the
service nginx restartcommand.