nginx301重定向
2016-06-14 22:44:57
18403
有時需把某目錄整個重定向到一個二級域名,或者不帶www的頂級域名,請求全部重定向到帶www的二級域名.?果是Apache,需要配置.htaccess,nginx不支持,需要在配置文件里面使用rewrite指令來實現(xiàn)。
1.頂級域名重定向到www
server {
server_name landui.com;
rewrite ^/(.*)$ http://lncdfzh.com.cn/$1 permanent;
}
如上配置,所以landui.com的請求?重定向到lncdfzh.com.cn,301重定向對SEO很有幫助.這個配置大家用的最多。
www二級域名重定向到頂級域名
server {
server_name lncdfzh.com.cn;
rewrite ^/(.*)$ http://lncdfzh.com.cn/$1 permanent;
}
頂級域名的權重會比www二級域名的權重高,有些seoer會要求運維一定要把www的請求轉到頂級域名,和上面的做法相反。
2.目錄重定向
if ( $request_filename ~ nginxtest/ ) {
rewrite ^ http://lncdfzh.com.cn/nginx/? permanent;
}
目錄跳轉新域名
if ( $request_filename ~ nginx/ ) {
rewrite ^ http://lncdfzh.com.cn/? permanent;
}