Overview
This article explains how to redirect a URL for a website running on an Nginx server.
Redirecting your site from HTTP to HTTPS
See this article instead if you need to redirect HTTP requests to HTTPS.
Creating a redirect
To create a redirect in Nginx:
- View the Nginx configuration file locations article to create your local /nginx/example.com directory.
- Change into this directory:
[server]$ cd ~/nginx/example.com
- Create a file named redirects.conf.
- Add the code from one of the examples below.
- Reload Nginx for the changes to take effect.
Redirecting 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; }
Redirecting an entire site
The following code redirects all requests to your website site to example.com:
if ($request_filename ~ /*){
rewrite ^ http://example.com? permanent;
}