Search code examples
nginxsubdomainhttp-redirect

Redirect 301 from subdomain to domain specific url with nginx


I have now two blog sites.

A - blog.domain.com - created on Tumblr

B - domain.com/blog/ - created on Wordpress (which is on different server) and serving by nginx proxy on domain.com

I want to redirect all my posts from A to B, so I will move my subdomain to B server and setup redirects for old posts urls.

How can I do that well in nginx ?

I have a little different urls on new blog, so I can't do this dynamically for all(I have only 30-35 urls on the old blog so it could be done by manually line by line redirection).

I think that settings below will be ok

server {
  server_name  blog.domain.com;
  location / {
    return 301 http://domain.com/blog/;
  }
  location /posts/123456/my-first-post {
    return 301 http://domain.com/blog/my-first-post-on-new-blog/;
  }
}

Is this a good way to do that ?

Many thanks for help in this case.


Solution

  • You could put the list of URIs into a map:

    map $uri $newuri {
        default                     /blog/;
        /posts/123456/my-first-post /blog/my-first-post-on-new-blog/;
        /posts/another/post         /blog/somewhere-on-new-blog/;
    }
    server {
        server_name  blog.domain.com;
        return 301 http://domain.com$newuri;
    }
    

    See this document for details.