A Complete Guide to Redirecting URLs With Nginx Web Server

A Complete Guide to Redirecting URLs With Nginx Web Server

By redirecting URLs, you can send visitors from one page URL to another automatically. They don’t need to take any action or click on a button for this. So when the need arises, these are an extremely feature to have.

If you’re using the Nginx web server, then it will make your job of setting up redirects even easier. In this article, we explain how to move ahead with the process.

Types of Redirects

There are various types of redirects you can use to redirect users. But in this context, only two types of redirects are relevant. Those are:

Temporary Redirect (302 redirect)

Permanent Redirect (301 redirect)

When it’s a temporary redirect, the page has been removed temporarily. So the next time a user visits that page, he will not be redirected. But in a permanent redirect, the page has moved permanently. So every time the user clicks on the old link, he or she will always get redirected.

How to Implement Redirects Using Nginx?

The Nginx is found in the document root directory and handled within the .conf file. That’s where you can locate it.

The directory path will be:
Code:
/etc/nginx/sites-available/directory_name.conf
At times, it can be in the /html directory where your site’s files are stored. In case your server is hosting multiple websites, then you might have to look in the /domain.com path. Whatever might be the case, you can locate it inside your .conf file.

In the /etc/nginx/sites-available directory path, you can find a default file. You can utilize this file to append your redirects. If you do not find the file inside of the directory, then you can create one and name it html.conf (or, domain.com.conf if your server is hosting multiple files).

You can use this command to create the conf file:

Code:
ln -s /etc/nginx/sites-available/domain.com.conf /etc/nginx/sites-enabled/domain.com.conf
With that said, let’s look at how to set up temporary and permanent redirects.

Temporary Redirect (302 redirect)

Code:
server {
# Temporary redirect to a page
rewrite ^/olderpage$ http://www.newdomain.com/newpage redirect;
}
Permanent Redirect (301 redirect)

Code:
server {
# Permanent redirect to an individual page
rewrite ^/olderpage$ http://www.newdomain.com/newpage permanent;
}
Permanent WWW to non-WWW Redirect

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

Code:
server {
# Permanent redirect to www
server_name domain.com;
rewrite ^/(.*)$ http://www.newdomain.com/$1 permanent;
}
There will be times where you need to change the domain name of the website. In such cases, you can set up the redirect to a new site URL.

You should use this command to redirect to a new URL:

Code:
server {
# Setting up Permanent redirect to new URL
server_name olderdomain.com;
rewrite ^/(.*)$ http://mynewdomain.com/$1 permanent;
}
You can also redirect to HTTPS which is secure and is being used by most modern websites. You should use the following command:

Code:
server {
# Redirecting to HTTPS
listen 80;
server_name mydomain.com www.mydomain.com;
return 301 https://thisexample.com$request_uri;
}
After you’ve made the changes, you should test prior to restarting the server. You can use the Nginx Syntax check for this purpose.

Use this command:
Code:
nginx -t
If it returns empty, then you’re good to go and restart the Nginx server. But if it returns something, you should fix it first.

To restart Nginx, use this command:

Code:
service nginx reload
If you’re using Cent OS, here’s the command you should be using:

Code:
systemctl restart nginx
So that’s how to set up the redirect with Nginx. If you face problems doing the same, get in touch with your hosting provider for assistance.
Author
kumkumsharma
Views
838
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top