Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Redirect URLs with Nginx

This guide shows how to send visitors from one address to another using Nginx. It needs root access, so it is for VPS and dedicated server users.

Web Hosting1 min read6 steps

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

  1. Connect with SSH.
  2. Look in /etc/nginx/conf.d/ or /etc/nginx/sites-available/. Find the file for your site.
  3. 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

  1. Test the settings:
nginx -t

This checks for mistakes. Do not go on if it shows an error.

  1. Reload Nginx:
systemctl reload nginx

This applies your changes without cutting visitors off.

  1. Open the old address in a private browser window.
Tip: Prefer 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 301 for permanent moves.
  • Run nginx -t before every reload.
  • Keep a backup of the file.