Nginx: 301 Redirect To A Domain Name

To redirect all requests from one domain to another in Nginx, you can add the following code to your Nginx configuration file:

server {
listen 80;
server_name olddomain.com;
return 301 $scheme://newdomain.com$request_uri;
}

This code listens on port 80 and checks the hostname of incoming requests. If the hostname is “olddomain.com”, it returns a 301 redirect to “newdomain.com” with the same request URI.

Remember to replace “olddomain.com” and “newdomain.com” with the actual domains you want to redirect from and to. After making the changes, restart Nginx to apply the changes.

Leave a Comment