Creating redirects with Nginx

To redirect a URL in a website running on an Nginx server, you must manually create a redirects.conf file. In this file will then add your redirect code.

Steps to create a redirect on an Nginx server

  1. View the configuration file location article to create your local /nginx/example.com directory.
  2. Create a file named redirects.conf in this /nginx/example.com directory. You can create this via SSH or FTP.
  3. Add one of the contents from the following sections. Make sure to change the domain name to your actual domain. 
  4. If you're on a Dedicated serverreload Nginx for the changes to take effect. If you're on a VPS (private server), restart your server in the panel. You can do this on the (Panel > 'VPS' > 'Dashboard') page by clicking the Restart button to the right of your server.

Redirecting a single file

if ($request_filename ~ oldfile.html){
	rewrite ^ http://example.com/newfile.html? permanent;
}
This redirects requests from example.com/oldfile.html to example.com/newfile.html.

Redirecting an entire site

if ($request_filename ~ /*){
        rewrite ^ http://example.com? permanent;
}
This redirects requests to your site to example.com. Change example.com to any site you'd like to redirect to.

Redirecting non-secure traffic to HTTPS

This redirect forces your site to load the HTTPS version of your site to ensure your visitor's connections are encrypted.

if ($server_port = 80) {
	rewrite ^/(.*)$ https://example.com/$1 permanent;
}

See also

Did this article answer your questions?

Article last updated PST.