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 your site from HTTP to HTTPS
View the following article for instructions on how to redirect your Nginx site from HTTP to HTTPS.