Nginx 301 Redirects for a Single Page or Entire Directory

A simple guide to writing 301 redirects in Nginx. If you are new to Nginx, then you probably used to use Apache plus .htaccess for your 301 redirects. This tutorial will provide you with example for applying the same logic used in the .htaccess 301 redirect writing the redirect rules in the Ningx config file.

Nginx Domain Redirects

How do I redirect a domain with Nginx?

Since website really don’t need www as a domain prefix anymore, a common redirect is making www go to the non www domain.

www domain to non-www domain redirect

NGINX Directory Redirect

What if you want to make an entire director redirect to a new directory? For example, if you had to change the location of your blog from to

Use the directive inside of the main server bock in your Nginx config file.

If you would like the entire directory to redirect to the top level domain (, use this directive.

Nginx Single Page 301 Redirect

This is the standard 301 permanent redirect to be used for redirecting one page to another. Be careful about adding too many of these as they will clutter your Nginx config file.

If you have any questions about using these directives please leave a comment and I’ll do my best to follow up. To find have a professional developer work on your Nginx server contact DigiMix Web Design New York to learn more about their Nginx and server side web development services. Thanks!