URLs redirection in nginx








Website developers sometimes use .htaccess rules to implement URL redirects for their websites.
However this does not work for Nginx web server powered websites.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to implement URLs redirects.

In this context, we shall look into the process of configuring redirects in Nginx.

Different ways of implementing Redirects?

Basically the easiest way of setting redirects are "temporary redirects" and "permanent redirects".

Temporary redirects are useful to serve URLs temporarily from a different location. It is particularly useful while performing maintenance to redirect users to the maintenance page.

However, permanent redirects indicate that the old URL no longer servers the content and the browsers should not attempt to access it anymore. These are particularly useful in situations like change in the domain names.

How to configure URLs redirects in Nginx?

In Nginx, the configuration file typically found in the document root directory of the sites, "/etc/nginx/sites-available/directory_name.conf", handles these redirects. Formats for some of the commonly used redirect codes are given below:

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;
}


Redirect to www permanently

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

 
Permanent Redirect an old URL to New URL

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


We have added the redirect using the rewrite directive. 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.

Redirect HTTP to HTTPS

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


Testing Nginx Configuration

After placing these rewrite rules, it is a good idea to test the configuration prior to running a restart. Nginx syntax can be checked with the -t flag to ensure there is not a typo present in the file.

nginx -t


If it does not return any result, then the syntax is correct. Thus we can reload Nginx for the redirects to take effect.

systemctl restart nginx


[Need support in configuring redirect URLs using Nginx? – We are available to help you today.]



Conclusion

This guide will show you the different ways with which you can implement URLs redirects using Nginx.


For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on LinuxAPT.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com