How to Setup 301 Redirect in Nginx?

301 redirect is very easy to setup if we are using Apache webserver, usually we do it via editing the .htaccess file, however if we switched to Nginx webserver then we will find that the old .htaccess 301 trick doesn’t work here. So, here is how to apply a 301 redirect in Nginx? It is almost as easy as we use it in .htaccess world, let’s start.

Setup 301 Redirect in Nginx

Nginx 301 Redirect Solutions

In Nginx there are some types of redirections like www or non www redirection, single page redirection, whole directory redirection and complete domain redirection to another domain. Let’s see all of them.

Redirect Non-www to www Redirect

server {
listen 80;
server_name domain.com;
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
}

Redirect www to Non-www Redirect

server {
listen 80;
server_name www.domain.com;
rewrite ^/(.*)$ http://domain.com/$1 permanent;
}

Also ReadNginx Redirection Non-www to www and www to Non-www

Single Page 301 Redirect

Sometimes you will need to redirect an entire page instead of the domain to avoid 404 errors, in this case you can use this code inside server block to redirect a single page.

server {
...
if ( $request_filename ~ oldpage/ ) {
rewrite ^ http://www.domain.com/newpage/? permanent;
...
}

Directory 301 Redirect

Alternatively you may need to rediret an old directory to a new one, in this case you can use this kind of solution, remember to place it inside server block configuration:

server {
...
if ( $request_filename ~ myolddirectory/.+ ) {
rewrite ^(.*) http://www.domain.com/mynewdirectory/$1 permanent;
...
}

Domain to Domain 301 Redirect with Posts

If you are planning to change your domain or changing business name then domain redirection is the only best solution to preserver same users on new domain.

server {
...
server_name domain.com www.domain.com; 
rewrite ^ $scheme://www.new-domain.com$request_uri permanent;
...
}

Domain to Domain 301 Redirect

It is same as above but it does not redirect page request to another domain’s page. It is useful when you are planning to redirect a complete website to just your another domain’s home page.

server {
...
server_name domain.com www.domain.com; 
rewrite ^ $scheme://www.new-domain.com;
...
}

If you have any other examples to share, feel free to comment below.

Category: .

Leave a Reply

Your email address will not be published. Required fields are marked *

Responses

  1. Rahul Avatar
    Rahul

    Hi, I like your post, but I have one doubt. I am using DigitalOcean for hosting and I have to move my site to another domain. So my question is, where I have to write this code in hosting files, as there is not .htaccess in nginx and in old site or new site?

    1. AtulHost Avatar

      Hi Rahul, first let me explain you the main game of .htaccess file. There is no .htaccess file support in nginx and if it is there it is non functional as nginx does not understand it. The next thing is that nginx have it’s all configurations in /etc/nginx/sites-available/default, you can edit this using nano editor tool and use the below rewrite code to old server block to redirect old site to new one,

      server {
      ...
      server_name domain.com www.domain.com;
      rewrite ^ $scheme://www.new-domain.com$request_uri permanent;
      ...
      }

      Make sure that this code will redirect domain as well as it’s all resources to new domain.

  2. Rahul Avatar
    Rahul

    I have copied this code to config file but still not redireted to new domain. I have to change CNAME or A Record for new domain, as I am only doing 301 to new domain.

    1. AtulHost Avatar

      Hi Rahul as you pasted the code it should work fine and do not forget to reload services once made the changes, because new settings wouldn’t reflect unless you restart nginx. Since this is server side redirection, there is no need for CNAME, A Record and 301 changes; just your new site should work as normal. I hope this will help you.

  3. Chris Avatar
    Chris

    Atul. Thank you for the article.

    What if you are migrating a site from an .aspx solution and want to redirect [$name].aspx to [$name]?

    1. Atul Avatar

      Welcome Chris for your feedback, and yes it is possible to play with extensions as well, just make sure that your new site’s permalink is okay and follow below edited code.

      Small Edit in Directory 301 Redirect Code,

      if ( $request_filename ~ myolddirectory.aspx.+ ) {
      rewrite ^(.*) http://www.domain.com/mynewdirectory$1 permanent;
      }

      I am sure it will work for you.

  4. Ali Avatar
    Ali

    I successfully set 301 redirection in nginx without any hassle. Thanks for this tutorial.

  5. Ramesh Singh Avatar
    Ramesh Singh

    Hi Atul,

    Any idea on redirecting subdomain to folder in the same domain. Want to redirect site abc.domain.com to domain.com/abc and also to keep the old files from sub domain. It is a wordpress blog which I want to migrate to /blog but not sure how to do it on Nginx. Please guide.

    1. AtulHost Avatar

      I think domain to domain redirects with the post will work with a small tweak. Just make sure that you keep the old site’s config file there and just add one rewrite line code there below server_name directive as mentioned below.

      server {
      ...
      server_name blog.domain.com; 
      rewrite ^ $scheme://domain.com/blog/$request_uri permanent;
      ...
      }