这里以2个域名为例,原使用域名www.demo.com,新域名为blog.whsir.com,要想所有www.demo.com中的内容转到blog.whsir.com,只是替换域名,其他内容不做改变。
当用户访问www.demo.com/a/b/1.html时,自动转到blog.whsir.com/a/b/1.html
方法一:
server { server_name www.demo.com; rewrite ^/(.*)$ https://blog.whsir.com/$1 permanent; }
|---------|--------------------------------------------------------------------------------------------| | 1 2 3 4 | server { server_name www.demo.com; rewrite ^/(.*)$ http://blog.whsir.com/$1 permanent; } |
方法二:
server { server_name blog.whsir.com www.demo.com; if ($host != 'blog.whsir.com') rewrite ^/(.*)$ https://blog.whsir.com/$1 permanent; }
|-----------|------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 | server { server_name blog.whsir.com www.demo.com; if ($host != 'blog.whsir.com') rewrite ^/(.*)$ http://blog.whsir.com/$1 permanent; } |