HTTP redirects in Nginx

A complete guide to configuring redirects in Nginx.

HTTP redirects tell browsers and search engine crawlers to go from one URL to another. They're used in all kinds of situations: moving to a new domain, forcing HTTPS, cleaning up URL structure, consolidating duplicate content, and more. Done right, redirects preserve your search rankings and keep users from hitting dead ends.

Nginx handles redirects through the built-in ngx_http_rewrite_module, which comes installed by default.

How to use?

Running a VPS or dedicated server gives you full control over your Nginx config — you can set up any redirect logic you need, test it, and iterate quickly.

Where to configure redirects

Nginx redirects live in the server's configuration files:

  • /etc/nginx/nginx.conf — the main configuration file
  • /etc/nginx/sites-available/site_name — per-site configuration
  • /etc/nginx/sites-enabled/ — symlinks to active configs from sites-available

Make your changes in sites-available, then always validate the syntax before reloading:

nginx -t

301 redirect from www to non-www

server {
    listen 80;
    server_name www.domain.com;
    rewrite ^ http://domain.com$request_uri? permanent;
}

server {
    listen 80;
    server_name domain.com;
    ...
}

301 redirect from non-www to www

server {
    listen 80;
    server_name domain.com;
    rewrite ^ http://www.domain.com$request_uri? permanent;
}

server {
    listen 80;
    server_name www.domain.com;
    ...
}

301 redirect from HTTPS to HTTP

server {
    listen 443;
    server_name www.domain.com;
    rewrite ^ http://www.domain.com$request_uri? permanent;
}

server {
    listen 80;
    server_name www.domain.com;
    ...
}

301 redirect from HTTP to HTTPS

For a new domain:

server {
    listen SERVER_IP:80;
    server_name www.domain.com;
    rewrite ^ https://www.domain.com$request_uri? permanent;
}

server {
    listen SERVER_IP:443 ssl;
    server_name www.domain.com;
    ...
}

For an existing domain:

server {
    listen SERVER_IP:80;
    server_name www.domain.com;
    rewrite ^ https://www.domain.com$request_uri? permanent;
}

Redirecting a specific page to a new URL

server {
    ...
    if ($request_filename ~ oldpage/) {
        rewrite ^ http://www.domain.com/newpage/? permanent;
    }
    ...
}

301 redirect for an entire directory

server {
    ...
    if ($request_filename ~ oldfolder/.+) {
        rewrite ^(.*) http://www.domain.com/newfolder/$1 permanent;
    }
    ...
}

Our products and services

Web HostingReliable hosting services for websites of any scale.
Order
VPSFlexible cloud infrastructure with full root access.
Order
Dedicated ServersBare metal servers for maximum performance.
Order

301 redirect from one domain to another

server {
    server_name domain.com www.old-domain.com;
    rewrite ^ $scheme://www.new-domain.com;
}

Remove trailing slashes from URLs

server {
    ...
    rewrite ^/(.*)/$ /$1 permanent;
    ...
}

location ~ .+/$ {
    rewrite (.+)/$ $1 permanent;
}

Redirect away from index.php

if ($request_uri ~* "^(.*/)index\.phpquot;) {
    return 301 $1;
}

Fix double slashes (//) in URLs

merge_slashes off;

location ~* "//" {
    rewrite ^(.*)//(.*)$ $1/$2;
    rewrite ^ $uri permanent;
}

After making any changes, reload Nginx to apply them:

service nginx restart

Help

If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!

Need help?Our engineers will help you free of charge with any question in minutesContact us