Dec 02, 2025
Ariffud M.
7min Read
An NGINX redirect is a configuration that routes web traffic from one URL to another, so visitors and search engines can land on the correct destination. Redirects are important for maintaining SEO rankings, avoiding duplicate content, and improving user experience.
NGINX supports two types of redirects: permanent redirects (HTTP 301), which indicate permanent URL changes, and temporary redirects (HTTP 302), which are used for short-term redirections.
In this article, we’ll guide you through setting up temporary and permanent redirects in NGINX. You’ll learn to use directives like return, rewrite, and try_files, explore common use cases, and implement best practices to avoid issues like redirect loops.
By the end, you’ll be able to refine your website’s configuration for better traffic management.
Download comprehensive HTTP status codes cheat sheet
Before setting up URL redirects in NGINX, make sure your server meets the following requirements:
sudo apt update sudo apt install nginx
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

To configure NGINX redirects, you’ll use three key directives: return, rewrite, and try_files. Each serves a specific purpose for effective URL redirection.
The return directive is the simplest and most efficient way to create redirects. It’s ideal for scenarios like redirecting a URL or an entire domain. This directive also processes redirects faster as it avoids unnecessary complexity.
return immediately stops processing further instructions and sends the specified HTTP status code and destination URL to the client. Its basic syntax is as follows:
return [status code] [URL];
For example, to redirect from an old domain to a new one:
server {
listen 80;
server_name www.old-domain.tld;
return 301 https://www.new-domain.tld$request_uri;
}You can also use return to redirect a single page like this:
location /old-page {
return 301 /new-page;
}The rewrite directive lets you perform more complex redirects using regular expressions (regex), which match patterns in the original URL and transform them into new URLs.
It’s best for cases where return is insufficient, such as dynamically redirecting URLs based on specific patterns or conditions. While rewrite is powerful, improper usage can lead to problems like redirect loops, so use it carefully.
Here’s its basic syntax:
rewrite regex replacement [flag];
For example, to redirect a URL based on a pattern:
location / {
rewrite ^/old/(.*)$ /new/$1 permanent;
}Another example adds a trailing slash to URLs:
rewrite ^([^.]*[^/])$ $1/ permanent;
In contrast, you can redirect URLs with a trailing slash to their non-slash versions:
rewrite ^/(.*)/$ /$1 permanent;
When choosing rewrite, append the permanent flag for SEO-friendly redirects and use last for internal rewrites.
With try_files, you can check for the existence of specific files or directories before serving a response or redirecting traffic. It’s beneficial for dynamic websites, fallback configurations, or redirecting requests when files are missing.
The try_files directive uses this syntax:
try_files path1 path2 ... fallback;
Here’s an example of redirecting requests for nonexistent files to a custom error page:
location / {
try_files $uri $uri/ /404.html;
}Similarly, to redirect all missing files to your website’s homepage:
location / {
try_files $uri $uri/ /index.html;
}If you want to redirect missing PHP files to an error handler:
location ~ .php$ {
try_files $uri /error-handler.php;
}NGINX redirects serve various purposes, from strengthening security to improving user experience. Here are five common use cases and instructions on how to implement them.
Redirecting HTTP traffic to HTTPS improves your website’s security and builds user trust. It also ensures search engines recognize your site as secure, which can boost SEO rankings.
The following example demonstrates how to set up an HTTP to HTTPS redirect using the return directive:
server {
listen 80;
server_name domain.tld www.domain.tld;
return 301 https://$host$request_uri;
}Make sure you’ve set up an SSL certificate on your server before enabling the redirect. Learn how to install a free SSL on your VPS if you haven’t done so already.
Redirecting non-WWW traffic to the WWW version of your website maintains a consistent URL structure and avoids duplicate content issues. You can achieve this in NGINX by adding a dedicated server block to handle non-WWW requests. For example:
server {
listen 80;
server_name domain.tld;
return 301 http://www.domain.tld$request_uri;
}If your site uses HTTPS, add a similar configuration to redirect non-WWW traffic over HTTPS:
server {
listen 443 ssl;
server_name domain.tld;
return 301 https://www.domain.tld$request_uri;
}Redirecting traffic to a maintenance page is common during site updates or temporary downtime. Using a location block, you can easily configure an NGINX URL redirect to inform visitors of ongoing maintenance.
For instance, to redirect all traffic to a maintenance page:
server {
listen 80;
server_name domain.tld www.domain.tld;
location / {
return 302 /maintenance.html;
}
location /maintenance.html {
root /var/www/html;
}
}Conditional redirects in NGINX let you redirect traffic based on specific conditions, such as user agents or IP addresses. Use the if directive within a server or location block to do so.
The following example redirects mobile users to a mobile version of your site:
server {
listen 80;
server_name domain.tld www.domain.tld;
if ($http_user_agent ~* "Mobile") {
return 302 https://m.domain.tld$request_uri;
}
}Another example displays how to block or redirect specific IPs:
server {
listen 80;
server_name domain.tld;
if ($remote_addr = 192.168.1.1) {
return 403;
}
}By redirecting URLs with query parameters, you can manage dynamic requests while preserving or modifying specific data in the URL. NGINX makes this possible using the if or rewrite directives, combined with variables like $request_uri and $arg_.
For example, to redirect a URL while preserving query parameters:
location /old-page {
return 301 /new-page?$query_string;
}Meanwhile, to redirect visitors based on a specific query parameter:
server {
listen 80;
server_name domain.tld;
if ($arg_promo = "true") {
return 301 /promo-page;
}
}Follow these best practices when setting up NGINX redirects to achieve optimal performance, avoid errors, and maintain a smooth user experience:
Avoid redirect loops
Redirect loops occur when a rule unintentionally redirects traffic back to itself or overlaps with other rules. These loops can slow down your server and frustrate users. Prevent redirect loops by using precise conditions in your server or location blocks to avoid conflicts.
This example is not safe because it redirects traffic back to itself:
server {
listen 80;
server_name domain.tld;
return 301 http://domain.tld$request_uri;
}Conversely, below is an example of a safe redirect:
server {
listen 80;
server_name www.domain.tld;
return 301 https://domain.tld$request_uri;
}Use wildcards or regex only if necessary
While wildcards and regular expressions provide flexibility, they can complicate configurations and impact performance. Use them only when simpler options, like the return directive, can’t handle your requirements.
For instance, avoid overly broad patterns like this:
location ~ .* {
return 301 /new-page;
}Instead, use specific patterns or conditions, as shown below:
location ~ ^/old-page[0-9]+$ {
return 301 /new-page;
}Combine redirect rules where possible
Consolidating similar redirects into a single block minimizes directives and improves clarity. For example:
location ~ ^/(old-page1|old-page2|old-page3)$ {
return 301 /new-page;
}Minimize chained redirects
Chaining multiple redirects slows down page loading and negatively impacts SEO. Always redirect traffic directly to its final destination. For instance, instead of chaining:
/page1 -> /page2 -> /page3
Use a direct redirect like this:
/page1 -> /page3
Test redirect rules before and after deploying
Always validate your redirects so that they’ll behave as intended. Use these commands to check for syntax errors before reloading your configuration:
sudo nginx -t sudo systemctl reload nginx
Hostinger VPS customers can also use Kodee to identify syntax errors in their NGINX configuration. Simply ask, “Can you check my current NGINX configuration for any errors?” and the tool will respond like this:

Alternatively, tools like curl can verify HTTP status codes and redirected URLs:
curl -I http://domain.tld/old-page
After deployment, use SEO tools like Google Search Console or Screaming Frog to ensure no broken links or redirect loops.
Besides setting up redirects, you can also use NGINX to configure a reverse proxy. Read our article to learn more.
In this article, we’ve explained how to set up various NGINX redirects, including redirects from HTTP to HTTPS and from non-WWW to WWW. You’ve also discovered best practices, such as how to avoid redirect loops, use regex wisely, and thoroughly test your redirects.
Furthermore, we’ve covered configuring redirects with query parameters and maintaining clean server blocks. With this knowledge, you can efficiently manage traffic and optimize your server configurations for performance and SEO.
If you still have questions about NGINX server-side redirects, feel free to use the comment box below.
An NGINX redirect is a server-side instruction that sends visitors from one URL to another. It helps manage traffic, improve SEO, and maintain correct routing, typically using status codes like 301 (permanent) or 302 (temporary).
To configure a simple redirect in NGINX, use the return directive within the relevant server block. For example:
server {
listen 80;
server_name old-domain.tld;
return 301 https://new-domain.tld$request_uri;
}
For a 301 redirect in NGINX, use the following syntax:
return 301 https://new-domain.tld$request_uri;
This permanently redirects visitors from the old URL to the new destination.
Comments
July 26 2022
How do I redirect old pages of my website (like, /contact-me/, /about-me/, /portfolio.html) to the main domain of mysite.com?
July 28 2022
Hi there! Let's say you wish to redirect /contact-me directory to the main domain in Nginx. For that you would need to use the following output: server{ ... location /contact-me { rewrite ^/contact-me(.*)$ https://www.yourdomainanme.com/$1 redirect; } ... } Hopefully this helps you! ?
June 12 2025
Hello. You say this rule is unsafe: server { listen 80; server_name domain.tld; return 301 https://domain.tld$request_uri; } But the redirect here is to https (443 ssl). So this rule wouldn't execute a second time as it only listens for http (80). Or am I missing something? Thank you
June 18 2025
Thank you so much for pointing that out! It should be "http", not "https". This has now been fixed <3