2012-07-12 137 views
1

我有一个运行WordPress的网站example.com。现在,我想搬到这个博客子域blog.example.com,但我也想以下几点:Nginx:将网站迁移到新域名

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。

回答

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

非常感谢!现在正在工作 – 2012-07-13 06:54:22

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 ..... 
} 
+0

当然,我有像这些blog.example.com的设置和block.example.com完美。问题是如何从example.com的根目录中删除重定向? – 2012-07-12 23:01:43

+0

很奇怪,上面的代码不工作..永久性重定向有时很棘手。您是否尝试使用其他浏览器?如果它以某种方式缓存了301响应... – Tisho 2012-07-12 23:17:17