How To Create Temporary and Permanent Redirects With Nginx

HTTP redirection is a way to point one domain or address to another. There are a few different kinds of redirects, each of which means something different to the client browser. The two most common types are temporary redirects and permanent redirects. In this article, we will explore how to implement each of these redirects in Nginx.

Temporary Redirects

Temporary redirects are useful if a URL temporarily needs to be served from a different location. For example, if you are performing site maintenance, you may wish to use a temporary redirect of from your domain to an explanation page to inform your visitors that you will be back shortly.

You can create a temporary redirect in Nginx by adding a line like this to the custom nginx.conf in the /public_html/ directory: 

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

 

Permanent Redirects

Permanent redirects inform the browser that it should forget the old address completely and not attempt to access it anymore. These are useful when your content has been permanently moved to a new location, like when you change domain names.

Likewise, You can create a temporary redirect in Nginx by adding a line like this to the custom nginx.conf in the /public_html/ directory: 

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

 

Conclusion

Now you know how to create temporary and permanent redirects within Nginx! Follow us on Facebook and Twitter for more WordPress guides and Tutorials.

Tagged

, , , ,

« | »