How to Redirect URLs using Nginx

By Tech-Assured     
May 5, 2021  2408 Views

How-to-Redirect-URL

If the need has arisen to redirect traffic from one URL to another, then you’ve come to the right place. We at Tech-Assured, provide network support as a part of our Server Management service.

Today we will show you how to Redirect URL’s using Nginx…

 

There are different types of Redirects but the common forms are Temporary & Permanent.

  • Temporary Redirects:
    This is really helpful, especially while performing maintenance in your company.
  • Permanent Redirects:
    This is helpful, when you have moved the content from your site and you don’t want your URL to access it anymore.

Solution


In Nginx, you can add following lines in the server configuration file:

For Temporary Redirects,

rewrite ^/oldlocation$ http://www.newdomain.com/newlocation redirect

For Permanent Redirects,

rewrite ^/oldlocation$ http://www.newdomain.com/newlocation permanent

Other Scenarios


In the document root directory of your site:

/etc/nginx/sites-available/directory_name.conf

 

Note: /html if you have one site on the server.

/domain.com if you have multiple sites on server

You can also create new file name html.conf or domain.com.conf

Few examples


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

 server { # Permanent redirect to www server_name domain.com; rewrite ^/(.*)$ http://www.newdomain.com/$1 permanent; }
server { # Permanent redirect to new URL server_name olddomain.com; rewrite ^/(.*)$ http://newdomain.com/$1 permanent; } server { # Redirect to HTTPS listen      80; server_name domain.com www.domain.com; return      301 https://example.com$request_uri; }

Ask for help –
There are different commands for different types of Redirects, but our Support Engineers at tech assured have a solution for all server related issues.

Tech-Assured can help you deploy best IT practices and mitigate risks with a fully compliant IT framework.


Enlink Clients