1

我有一个运行 wordpress 的网站 example.com。现在我想将此博客移动到子域 blog.example.com,但我还想要以下内容:

example.com --> static page (not wordpress)

blog.example.com --> new address to the blog
blog.example.com/foo --> handled by wordpress

example.com/foo --> permanent redirect to blog.example.com/foo

所以我尝试了下一个配置:

    server {
            server_name example.com;

            location = / {
                    root /home/path/to/site;
            }

            location / {
                    rewrite ^(.+) http://blog.example.com$request_uri? permanent;
            }
    }

在这种情况下,重定向工作得很好。不幸的是 example.com 也重定向到 blog.example.com。

4

2 回答 2

2

它重定向的原因是因为当它尝试加载 example.com 的索引文件时,它执行到 /index.html 的内部重定向,该重定向由您的重写位置处理。为避免这种情况,您可以使用 try_files:

server {
  server_name example.com;

  root /home/path/to/site;

  location = / {
    # Change /index.html to whatever your static filename is
    try_files /index.html =404;
  }

  location / {
    return 301 http://blog.example.com$request_uri;
  }
}
于 2012-07-12T23:07:38.547 回答
1

只要两个域的根目录指向不同的目录,你就需要两个server指令——像这样:

server {
        # this is the static site
        server_name example.com;

        location = / {
                root /home/path/to/static/page;
        }

        location /foo {
                return 301 http://blog.example.com$request_uri;
        }
}

server {
        # this is the WP site
        server_name blog.example.com;

        location = / {
                root /home/path/to/new_blog;
        }

        .... some other WP redirects .....
}
于 2012-07-12T22:44:35.613 回答