【问题标题】:Redirect 301 from subdomain to domain specific url with nginx使用 nginx 将 301 从子域重定向到特定于域的 url
【发布时间】:2017-06-15 12:59:16
【问题描述】:

我现在有两个博客网站。

A - blog.domain.com - 在 Tumblr 上创建

B - domain.com/blog/ - 在 Wordpress 上创建(在不同的服务器上)并由 domain.com 上的 nginx 代理提供服务

我想将我所有的帖子从 A 重定向到 B,所以我会将我的子域移动到 B 服务器并为旧帖子 URL 设置重定向。

如何在 nginx 中做到这一点?

我在新博客上有一些不同的网址,所以我不能为所有人动态执行此操作(我在旧博客上只有 30-35 个网址,因此可以通过手动逐行重定向来完成)。

我认为下面的设置就可以了

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

这是一个好方法吗?

非常感谢您在这种情况下的帮助。

【问题讨论】:

    标签: redirect nginx subdomain url-redirection


    【解决方案1】:

    您可以将 URI 列表放入地图中:

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

    详情请见this document

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2012-01-19
      • 2013-09-26
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 2018-02-15
      相关资源
      最近更新 更多