下面示例将HdhCmsTestexample测试数据跳转到example测试数据,如果你需要跳转到www域名,相应修改一下下面代码即可。Nginx 301跳转 设置 其实很简单,首先打开站点.conf配置文件,文件路径一般是:/usr/local/nginx/conf/vhost/,然后下载本地或者使用vi修改都行。
找到server代码段,如下相应修改即可:
server { listen 80; server_name HdhCmsTestexample测试数据 example测试数据; if ($host ~* HdhCmsTestexample测试数据) { rewrite ^/(.*)$ http://example测试数据/$1 permanent; } index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/example测试数据; include none.conf; location ~ .*\.(php|php5)?$ { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } access_log /home/wwwlogs/example测试数据.log example测试数据; } 其中上面添加到跳转代码是:
if ($host ~* HdhCmsTestexample测试数据) { rewrite ^/(.*)$ http://example测试数据/$1 permanent; } 设置后重启Nginx即可生效,可以使用 http://tool.chinaz测试数据/pagestatus/ 查询一下返回状态码是否是301。如果跳转访问错误,请确保你域名有正确解析。