How to Redirect URLs using NGINX? (Temporary & Permanent)



What is redirection?

Regarding Nginx, redirection is a web server function in which website traffic is moved from one URL to another. It is one of the most useful features through which you can easily redirect visitors to another location automatically.

Although, there are numerous types of redirections you can do, the two most important and commonly used are permanent (301) and temporary redirection (302).

In this post, we will show you redirection methods with examples such as, from www to non-www, Nginx redirection to a Domain, Nginx redirection from HTTP to HTTPS, etc.


Read: 🚩 How does WordPress Redirect URL work? 5 Best Methods


What is the need for Nginx Redirection?

  • Nginx provides one of the crucial features i.e redirecting website URLs to another address based on criteria.
  • Nginx redirects are very user-friendly and easy to configure, due to which it is very flexible to manage website URLs.
  • It is very useful in preserving SEO ranking for a page especially if you’re migrating from one CMS to another.
  • The redirection feature also helps to retain the SERP position despite changes in the URL structure.

Common Nginx Redirection Methods (Temporary and Permanent)

As we have already mentioned there are multiple types of redirection methods available, but in practice, there are only two commonly used, i.e Temporary and Permanent redirection.

Temporary Redirection (Response code: 302 Found)

  • From the name itself, you can anticipate what temporary medication will do. Well, this method serves the purpose of temporarily changing a page’s location from one place to another.
  • The Response code: 302 Found is employed to show the temporary forwarding of a page during these redirects.
  • It is mostly used during website maintenance; temporary redirection informs traffic or users about the website’s temporary unavailability.
  • It can be also used to redirect an incomplete page to another point or home page.
  • Example scenario: Visitor navigates to the Website Page, but due to maintenance, they are redirected to a notification indicating that the website is currently under maintenance.
Temporary Redirection
Temporary Redirection (Response code: 302 Found)

Permanent Redirection (Response code: 301 Found)

  • The Permanent Nginx Redirect is used to permanently relocate a page domain.
  • A 301 redirect is employed to designate the permanent movement of a page in this context.
  • This redirection is mainly used when a user wants to change a domain name and no longer wants website traffic to access the old location.
Permanent Redirection
Permanent Redirection (Response code: 301 Found)

How to redirect URLs using Nginx (Temporary and permanent)

Before we start, first you need to make sure that NGINX is installed on your VPS or Virtual Private Server. After that access your VPS via SSH. At Wpoven you can easily access your site via SSH command line console.


Read: 🚩 Can I access my WPOven Site via SSH


Configuration Setup of NGINX

  • NGINX Configuration File Location: The configuration for NGINX is typically stored in a .conf file, which is a plain text file containing directives and settings for the server.
  • Default Location: The default location for NGINX configuration files is often within the document root directory of your site(s). In this case, it mentions the path: /etc/nginx/sites-available/directory_name.conf.
  • Document Root Directory: This directory is where your website’s files are stored. The document root can vary; for a single-site server, it might be in the /html directory. If your server hosts multiple sites, the document root might be within a specific domain directory like /domain.com.
  • Configuration File Naming: The name of your .conf file corresponds to your site. For example, if your site is in the /html directory, the .conf file might be named html.conf. If it’s in a directory like /domain.com, then the .conf file could be named domain.com.conf.
  • Default Configuration File: Within the /etc/nginx/sites-available/directory, there is usually a default file that you can use as a template. This file might contain basic configurations that you can copy or modify to suit your needs.
  • Creating a New Configuration File: Alternatively, you have the option to create a new .conf file. For instance, you could create a file named html.conf or domain.com.conf based on your site’s structure and naming conventions.

You might be surprised to know that, in Nginx, there is an inbuilt tool available called the “rewrite” directive. It is a very handy tool that makes redirecting easy. The best part is there is nothing special you need to do about it, it’s automatically present whenever you install a a fresh Nginx.

This “rewrite” directive is not just meant for doing basic redirections, but it’s quite versatile. i.e you will be able to create two types of redirects, permanent and temporary.

All you have to do is pass only two arguments, the old URL and the New URL. Below are some examples for your reference. Just, enter the following lines in your server configuration.

First, open the configuration file by entering the following command in the terminal.

$ sudo vi /etc/nginx/nginx.conf

after that use the following given below commands as per your requirement.

1. Page to Page Nginx Redirection

Temporary Page to page Nginx redirect

server {
# Temporary redirect to an individual page
rewrite ^/oldpage$ http://www.example.com/newpage redirect;
}

Permanent Page to Page Nginx redirect

server {
# Permanent redirect to an individual page
rewrite ^/oldpage$ http://www.example.com/newpage permanent;
}

From the above commands, you have noticed that if you want to create a permanent redirect, all you need to do is replace “redirect” with “permanent” at the end of the directive.

2. WWW to non-www Nginx Redirection

Permanent www to the non-www redirection:

server {
# Permanent redirect to non-www
server_name www.example.com;
rewrite ^/(.*)$ http://example.com/$1 permanent;
}


Note: In case you like to redirect from non-www to www, just you need to replace the website URLs mentioned in the above command. i.e Replace www.example.com with example.com and vice versa.


3. How to redirect the URL from HTTP to HTTPS in Nginx?

Before doing this redirection, do not forget to install the SSL certificate. Redirecting to HTTPS has its benefits. It helps to protect your website from all MIMT attacks, data theft, etc. And even their ports are different, for example, HTTP uses 80 Port whereas, HTTPS uses 443.

So to protect all the sensitive information sharing between you and your visitor you need the following commands:

server {
listen 80;
server_name www.domain.tld;
return 301 https://www.domain.tld$request_uri;
}

4. Nginx Redirect URL to another URL

If you wish to permanently redirect i.e. nginx redirect to another URL 301 use the following command:

To a new domain from an older domain,

server {
listen 80;
listen 443 ssl;
server_name olddomain.com www.olddomain.com;
return 301 $scheme://www.newdomain.com$request_uri;
}

OR

server {
# Permanent redirect to new URL
listen 443 ssl;
# Add this line to listen on the HTTPS port and enable SSL server_name olddomain.com;
ssl_certificate /path/to/your/ssl_certificate.crt; # Update with the actual path to your SSL certificate
ssl_certificate_key /path/to/your/ssl_certificate_key.key; # Update with the actual path to your SSL certificate key
# Additional SSL settings can be configured here
rewrite ^/(.*)$ https://newdomain.com/$1 permanent;
# Update to use 'https' protocol
}

  • We’ve set up a redirection for our website using a tool called the “rewrite” directive in Nginx.
  • The ^/(.*)$ is a regular expression that will use everything after the last slash ( / ) in the URL.
  • For example, if someone tries to access http://olddomain.com/index.html, it will use the index.html part.
  • So, if you were going to http://olddomain.com/index.html, it will smoothly redirect you to http://newdomain.com/index.html.
  • The idea is to seamlessly redirect users to the same content on the new domain.
  • To make sure this redirect is permanent, we’ve added a special keyword, “Permanent” After the rewrite directive.
  • This tells both browsers and search engines that this is a permanent change, using what’s called an HTTP 301 status code.

5. Nginx redirects to another domain without changing the URL

Suppose you want to redirect the domain olddomain.com to newdomain.com without changing the URL. It means that when you enter olddomain.com, it should redirect to newdomain.com, but the URL should still display olddomain.com.

Here is how you can approach,

server {
listen 80;
server_name olddomain.com;

location / {
proxy_pass http://newdomain.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}
}

6. Specific Site Nginx Redirect

server {
listen 80;
server_name example.com;
return 301 http://newexample.com$request_uri;
}


Check Nginx Syntax

To check all the nginx Syntax are correct, use the following command:

nginx -t

If the output turns out to be empty, it means the syntax is correct and you need to restart the Nginx Server to make these modifications effective.


Restart Nginx Server

Do not forget to restart your Nginx server to make these rules effective by using the command:

sudo systemctl restart Nginx


Summary

As already mentioned above, Nginx turned out to be the most powerful and user-friendly web server that lets you make permanent and temporary Nginx redirects easily.

After reading this blog, now you know How to create Redirect URLs using NGINX. Whether, to nginx redirect from HTTP to HTTPS, from one domain to another domain, etc.

Also, you have learned how redirections affect your SEO rankings, so always make sure you use the correct redirection type.

With this helpful feature, you can easily change your website structure as per trends and requirements and refrain from fear of traffic and SERP rankings loss.

WPOven offers WordPress-managed hosting options for your next NGINX project. Contact us to discuss the best options for you.

If you have any queries or suggestions, please do let us know in the comment section below:


Frequently Asked Questions

How to redirect a URL in nginx?

In Nginx, there is an inbuilt tool available called the “rewrite” directive. It is a very handy tool that makes redirecting easy. The best part is there is nothing special you need to do about it, it’s automatically present whenever you install a a fresh Nginx.

What is the redirect code for Nginx?

The nginx is quite versatile and offers you two types of redirections, permanent whose response code is 301, and temporary reduction response code is 302.

What is NGINX used for?

Nginx is a versatile and high-performance web server and reverse proxy server. It is commonly used for the following purposes: Web Server, Reverse Proxy Server, Load Balancer, SSL/TLS Termination, Caching, Content Delivery Network (CDN), API Gateway, WebSocket Support, Security, and High Availability Architecture


Leave a Reply

Your email address will not be published. Required fields are marked *