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