Creating redirects with Nginx

Overview

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 VPS 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 your site from HTTP to HTTPS

View the following article for instructions on how to redirect your Nginx site from HTTP to HTTPS.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?