Creating redirects with Nginx

Follow these steps to create your redirects:

  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.
  3. Add the contents from the following sections.
  4. Make sure to reload Nginx for the changes to take effect.

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

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

See also

Did this article answer your questions?

Article last updated .