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
- View the configuration file location article to create your local
/nginx/example.com
directory. - Create a file named redirects.conf in this
/nginx/example.com
directory. You can create this via SSH or FTP. - Add one of the contents from the following sections. Make sure to change the domain name to your actual domain.
- If you're on a Dedicated server, reload 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 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; }