Creating redirects with Nginx

 

Overview

Nginx can redirect requests from one URL to another for a website running on an Nginx server, either for a single file or for an entire site.

Redirecting your site from HTTP to HTTPS

See this article instead if you need to redirect HTTP requests to HTTPS.

How do I create a redirect?

To create a redirect in Nginx:

DreamPress plans do not support changes to the local Nginx configuration directory, meaning configuration adjustments cannot be made directly by the user.

If you need to customize the redirects.conf file, please contact support for assistance.

  1. View the Nginx configuration file locations article to create your local /nginx/example.com directory.
  2. Change into this directory:
    [server]$ cd ~/nginx/example.com
  3. Create a file named redirects.conf.
  4. Add the code from one of the examples below.
  5. Reload Nginx for the changes to take effect.

How do I redirect a single file?

The following code redirects requests from example.com/oldfile.html to example.com/newfile.html:

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

How do I redirect an entire site?

The following code redirects all requests to your website site to example.com:

if ($request_filename ~ /*){
    rewrite ^ http://example.com? permanent;
}

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?