What is a redirect?
A redirect tells the browser, "This page moved, go here instead." Nginx is the web server that answers visitors. Its rules live in settings files, and Nginx does not read .htaccess files.
Step 1: Find your settings file
- Connect with SSH.
- Look in
/etc/nginx/conf.d/or/etc/nginx/sites-available/. Find the file for your site. - Back it up:
cp /etc/nginx/conf.d/example.conf /etc/nginx/conf.d/example.conf.bak
This saves a safe copy so you can go back if something breaks.
Step 2: Add a rule
Open the file in an editor such as nano. Rules go inside a server { } block.
Redirect one page:
location = /old-page {
return 301 /new-page;
}
The number 301 means the move is permanent. Use 302 for a temporary move.
Redirect a whole domain to another:
server {
listen 80;
server_name old-example.com www.old-example.com;
return 301 https://new-example.com$request_uri;
}
The $request_uri part keeps the rest of the address, so each page lands on its matching page.
Redirect HTTP to HTTPS:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
HTTPS is the safe version of a web address, with encryption. This rule sends everyone there.
Step 3: Test and reload
- Test the settings:
nginx -t
This checks for mistakes. Do not go on if it shows an error.
- Reload Nginx:
systemctl reload nginx
This applies your changes without cutting visitors off.
- Open the old address in a private browser window.
return over rewrite for simple moves. It is faster and easier to read.Quick recap
- Nginx redirects live in the server settings, not in .htaccess.
- Use
return 301for permanent moves. - Run
nginx -tbefore every reload. - Keep a backup of the file.