How to Redirect URLs Using NGINX

Posted on by Ian White | Updated:
Reading Time: 3 minutes

What is a Redirect?

A redirect is a web server function that will redirect traffic from one URL to another. Redirects are an important feature when the need arises. There are several different types of redirects, but the more common forms are temporary and permanent. In this article, we will provide some examples of redirecting through the vhost file, forcing a secure HTTPS connection, redirection to www and non-www as well as the difference between temporary and permanent redirects.

Note
As this is an NGINX server, any .htaccess rules will not apply. If your using the other popular web server, Apache, you’ll find this article useful.

Common Methods for Redirects

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

However, permanent redirects (response code: 301 Moved Permanently) inform the browser there was an old URL that it should forget and not attempt to access anymore. These are helpful when content has moved from one place to another.

How to Redirect

When it comes to NGINX, that is handled within a .conf file, typically found in the document root directory of your site(s), /etc/nginx/sites-available/directory_name.conf. The document root directory is where your site's files live, and it can sometimes be in the /html directory if you have one site on the server. Or if your server has multiple sites it can be at /domain.com.  Either way that will be your .conf file name. In the /etc/nginx/sites-available/directory you'll find the default file that you can copy or use to append your redirects. Or you can create a new file name html.conf or domain.com.conf.

Note
If you choose to create a new file be sure to update your symbolic links in the /etc/nginx/sites-enabled. With the command: ln -s /etc/nginx/sites-available/domain.com.conf /etc/nginx/sites-enabled/domain.com.conf

The first example covered is the redirection of a specific page or directory to a new one.

Temporary Page-to-Page Redirect

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

Permanent Page-to-Page Redirect

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

Permanent www to non-www Redirect

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

Permanent Redirect to www

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

Sometimes the need will arise to change the domain name for a website. In this case, a redirect from the old site's URL to the new site's URL will be very helpful in letting users know the domain was moved to a new URL.

The next example we’ll cover is redirecting an old URL to a new URL.

Permanent Redirect to New URL

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

We've added the redirect using the rewrite directive we discussed earlier. The^/(.*)$ regular expression will use everything after the / in the URL. For example, http://olddomain.com/index.html will redirect to http://newdomain.com/index.html. To achieve the permanent redirect, we add permanent after the rewrite directive as you can see in the example code.

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

Redirect to HTTPS

server {
 # Redirect to HTTPS
 listen      80;
 server_name domain.com www.domain.com;
 return      301 https://example.com$request_uri;
 }

After these rewrite rules are in place, testing the configuration prior to running a restart is recommended. NGINX syntax can be checked with the -t flag to ensure there is not a typo present in the file.

NGINX Syntax Check

nginx -t

If nothing is returned the syntax is correct, and NGINX has to be reloaded for the redirects to take effect.

Restarting NGINX

service nginx reload

CentOS 7

Unlike CentOS 6, CentOS 7 uses systemd.

systemctl restart nginx

Redirects on Managed WordPress/WooCommerce

If you are on Liquid Web's legacy Managed WordPress/WooCommerce products, redirects can happen through the /home/s#/nginx/redirects.conf file. Each site will have its own s# which is the FTP/SSH user per site. The plugin called Redirection can be downloaded to help with a simple page-to-page redirect, otherwise the redirects.conf file can be utilized in adding more specific redirects as well using the examples explained above. Due to the nature of a managed platform after you have the rules in place within the redirects.conf file, please reach out to support and ask for NGINX to be reloaded.

Conclusion

Using NGINX to redirect URLs can be a powerful and effective way to manage and optimize website traffic. However, understanding the basics of NGINX and the configuration options available when setting up URL redirects is also essential. Additionally, it is vital to consider the potential implications of the redirects and ensure proper setup.

It is beneficial to monitor the performance of the redirects and make necessary adjustments. With the appropriate knowledge and implementation, NGINX can be a powerful tool for managing and optimizing the user experience.

Liquid Web offers managed hosting options for your next NGINX project. Contact the sales team to discuss the best options for your needs.

Avatar for Ian White

About the Author: Ian White

Latest Articles

Blocking IP or whitelisting IP addresses with UFW

Read Article

CentOS Linux 7 end of life migrations

Read Article

Use ChatGPT to diagnose and resolve server issues

Read Article

What is SDDC VMware?

Read Article

Best authentication practices for email senders

Read Article