redirect url in nginx

How to Redirect URL in NGINX

When you move a page on your website, you need to redirect the specific URL to another URL. Here’s how to redirect URL in NGINX.

 

How to Redirect a URL in NGINX

NGINX is a powerful web server that offers many useful features such as Reverse Proxy and Caching. Here are the steps to redirect URL in NGINX. After you redirect URL in NGINX, you may want to use a reporting software to monitor the key metrics about your website/application such as signups, traffic, sales, revenue, etc. using dashboards & charts, to ensure everything is working well.

 

1. Open NGINX config file

If you are using NGINX’s main configuration file nginx.conf, without virtual hosts, then run the following command

$ sudo vi /etc/nginx/nginx.conf

 

If you have configured separate virtual hosts for your website (e.g www.domain.com), such as /etc/nginx/sites-enabled/domain.conf then open it with the following command

$ sudo vi /etc/nginx/sites-enabled/domain.conf

 

2. Redirect URL in NGINX

There are multiple ways to redirect URL in NGINX. Let’s look at them one by one.

If you want to temporarily redirect URL to another URL in NGINX, then add the following server block to your configuration file.

server {
# Temporary redirect to an individual page
rewrite ^/old-url$ http://www.domain.com/new-url redirect;
}

If you want to permanently redirect URL to a new one, then add the following server block to your configuration file.

server {
# Permanent redirect to an individual page
rewrite ^/old-url$ http://www.domain.com/new-url permanent;
}

 

If you want to permanently redirect URL to its non-www version, then add the following server block to your configuration file.

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

 

If you want to permanently redirect URL to its www version, then add the following server block to your configuration file.

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

 

If you have moved your website to a new domain (e.g newdomain.com), and want to redirect URL to this new location, then add the following server block to your configuration file.

server {
# Permanent redirect to new URL
server_name domain.com;
rewrite ^/(.*)$ http://newdomain.com/$1 permanent;
}

Similarly, you can also redirect location to another domain or redirect 404 to URL in NGINX

3. Check Syntax and Restart NGINX

Run the following command to check syntax of your updated config file.

$ sudo nginx -t

 

If there are no errors, run the following command to restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

 

That’s it! Now your NGINX server will automatically redirect URLs to their new location.

By the way, if you want to create charts & dashboards to monitor your business or website, you can try Ubiq. We offer a 14-day free trial.

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!