HTTP Redirects in Nginx
A Complete Guide to Setting Up Redirects in Nginx
HTTP redirects are an important tool for managing web resources. They are used to unify site mirrors, switch users from HTTP to HTTPS, reorganize page structures, or when changing domains. Properly configured redirects improve user experience and help maintain a site’s search engine rankings. In this article, we’ll look at how to implement different types of redirects in Nginx using standard directives.
Note
An HTTP redirect is a mechanism that forwards a request from one domain or URL to another. It’s used whenever you need to send users to a new address. Typical use cases include fixing incorrect URLs, enforcing HTTPS, or moving content to a new location.
Redirects in Nginx are configured using the ngx_http_rewrite_module, which is included by default with the server.
Redirects are set up in Nginx configuration files. The main files are:
/etc/nginx/nginx.conf
— the main configuration file;/etc/nginx/sites-available/site_name
— configuration for a specific site;/etc/nginx/sites-enabled/
— a symlink to the active file fromsites-available
.
Note
It’s best practice to edit files in the sites-available
directory, then check the syntax with:
nginx -t
301 redirect from www.domain.ru
to domain.ru
server {
listen 80;
server_name www.domain.ru;
rewrite ^ http://domain.ru$request_uri? permanent;
}
server {
listen 80;
server_name domain.ru;
...
}
301 redirect from domain.ru
to www.domain.ru
server {
listen 80;
server_name domain.ru;
rewrite ^ http://www.domain.ru$request_uri? permanent;
}
server {
listen 80;
server_name www.domain.ru;
...
}
301 redirect from HTTPS to HTTP
server {
listen 443;
server_name www.domain.ru;
rewrite ^ http://www.domain.ru$request_uri? permanent;
}
server {
listen 80;
server_name www.domain.ru;
...
}
301 redirect from HTTP to HTTPS
For a new domain:
server {
listen server_ip:80;
server_name www.domain.ru;
rewrite ^ https://www.domain.ru$request_uri? permanent;
}
server {
listen server_ip:443 ssl;
server_name www.domain.ru;
...
}
For an existing domain:
server {
listen server_ip:80;
server_name www.domain.ru;
rewrite ^ https://www.domain.ru$request_uri? permanent;
}
Redirect a single URL
server {
...
if ($request_filename ~ oldpage/) {
rewrite ^ http://www.domain.ru/newpage/? permanent;
}
...
}
301 redirect for a directory
server {
...
if ($request_filename ~ oldfolder/.+) {
rewrite ^(.*) http://www.domain.ru/newfolder/$1 permanent;
}
...
}
301 redirect from one domain to another
server {
server_name domain.com www.domain.ru;
rewrite ^ $scheme://www.new-domain.ru;
}
Remove trailing slash from URLs
server {
...
rewrite ^/(.*)/$ /$1 permanent;
...
}
location ~ .+/$ {
rewrite (.+)/$ $1 permanent;
}
Redirects from index.php
pages
if ($request_uri ~* "^(.*/)index\.phpquot;) {
return 301 $1;
}
Redirects from URLs with double slashes (//
)
merge_slashes off;
location ~* "//" {
rewrite ^(.*)//(.*)$ $1/$2;
rewrite ^ $uri permanent;
}
Important
After making changes, don’t forget to restart the server:
service nginx restart
Note
Using a VPS or a dedicated server gives you full control over the Nginx configuration. This allows you to manage redirects flexibly, test different setups, and quickly fix any issues.