Knowledge Base
MilesWeb / Web Hosting FAQ

Steps to Redirect URLs Using Nginx

Approx. read time : 3 min

What is a Redirect?

A web server function that redirects traffic from one URL to another is called as redirect. It is an important feature when there is a need of it. You will find several types of redirects, but those commonly used are temporary and permanent. In this article, we will offer some examples of redirecting via the vhost file, forcing a HTTPS connection redirection to www and non-www, and the difference between temporary and permanent redirects.

Note: No .htaccess rules will apply as this is an Nginx server.

Common Methods for Redirects

If a URL is temporarily being served from a different location, temporary redirects (response code: 302 Found) are helpful. For example, these are helpful for maintenance and can redirect users to a maintenance page.

Permanent redirects (response code: 301 Moved Permanently) notify the browser about the old URL that it should forget and not attempt to access further. These redirects prove helpful when content has moved from one place to another.

How to Redirect?

Nginx is handled within a .conf file located in the document root directory of your site(s), /etc/nginx/sites-available/directory_name.conf. Your site’s files live on the document root directory and it can be in the /html, if you have a single site on the server. In case, there are multiple sites on your server, the root directory can be at /

May it be anything, it will be your .conf file name. You will find the default file in the /etc/nginx/sites-available/ directory that can be copied or used to append your redirects. Or a new file name html.conf or can be created.

Note: In case you select to create a new file ensure to update your symbolic links in the /etc/nginx/sites-enabled with the below command:

First we will cover the example of redirection of a specific page/directory to the new page/directory.

Temporary Page to Page Redirect

Permanent Page to Page Redirect

Permanent www to non-www Redirect

Permanent Redirect to www

Sometimes you will need to change the domain name for a website. Therefore, a redirect from the old sites URL to the new sites URL will be very helpful in helping users know the domain was moved to a new URL.

Now we will check the example of redirecting an old URL to a new URL.

Permanent Redirect to New URL

The redirect is added using the rewrite directive discussed earlier. The ^/(.*)$ regular expression will use everything after the / in the URL. For example, will redirect to For permanent redirect, we add permanent after the rewrite directive as seen in the example code.

In terms of HTTPS and being fully secure, it is ideal for forcing everyone to use https:// instead of http://.

Redirect to HTTPS

Now the rewrite rules are in place and it is recommended to test the configuration prior to running a restart. You can check Nginx syntax with the -t flag to ensure there is not a typo present in the file.

Nginx Syntax Check

In case nothing is returned the syntax is correct and Nginx has to be reloaded for the redirects to take effect.

Restarting Nginx

For CentOS 7 which unlike CentOS 6, uses systemd:

Redirects on Managed WordPress/WooCommerce

If your website is running on Nginx web server, you can set the redirects through the /home/s#/nginx/redirects.conf file. Each site will have their own s# which is the FTP/SSH user per site. You can download the plugin called ‘Redirection’ to help with a simple page to page redirect, or else you can utilize the redirects.conf file for adding more specific redirects or you can even do that using the examples explained above.

After having set the rules properly within the redirects.conf file, please contact our support and ask to reload Nginx. In case you aren’t comfortable with performing the above steps, contact our support team via chat, ticket or a phone call. Our support team is available 24/7 to help you with this.

Also Read:

Learn to Configure Redirects in cPanel
Learn to Redirect HTTP to HTTPS in WordPress with Simple Steps

Pallavi Godse
Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.