How to Redirect to a Different Domain Using NGINX


redirect to new domain nginx

When you move your web pages or your entire website to a new domain, you will need to redirect your old pages to a new domain so that website visitors and search engines know that it had been moved. Let us take a look at how to redirect to a different domain using NGINX.

 

How to Redirect to a Different Domain Using NGINX

Here are the steps to redirect to a different domain using NGINX.

 

1. Open NGINX Config File

Open NGINX config file in a text editor. It is typically found at /etc/nginx/nginx.conf. You an update the location as per your requirement.


$ sudo vim /etc/nginx/nginx.conf

 

2. Update Server Block

Locate the server block and update it as shown below. Replace old_domain.com and new_domain.com to your requirement.


server {
 server_name .old_domain.com;
return 301 http://wwwnew_domain.com$request_uri;
}

 

If you have NGINX<0.9.1, then you can add the following


server {
 server_name .old_domain.com;
rewrite ^ http://www.new_domain.com$request_uri? permanent;
}

 

3. Reload NGINX Server

Reload NGINX server to apply changes.


$ sudo service nginx reload

 

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!