還是以前在Apache上做過(guò)的事,實(shí)現(xiàn)2種情況的需求
方法很簡(jiǎn)單,把下面的代碼按自己的需要加入/usr/local/nginx/conf/nginx.conf 的server段或location段中即可
需要小注意的一點(diǎn)是,如果你的網(wǎng)站是在子目錄下的,你可能需要增加 "location /子目錄/ {}" 段來(lái)實(shí)現(xiàn)子目錄的域名統(tǒng)一定向
1、當(dāng)用戶(hù)訪(fǎng)問(wèn)頂級(jí)域名 slyar.com/***.html 的時(shí)候,自動(dòng)轉(zhuǎn)向帶www的 www.slyar.com/***.html 下
if ($host = 'slyar.com' ) {
rewrite ^/(.*)$ http://www.slyar.com/$1 permanent;
}
2、當(dāng)用戶(hù)訪(fǎng)問(wèn)帶www的 www.slyar.com/***.html 的時(shí)候,自動(dòng)轉(zhuǎn)向不帶www的頂級(jí)域名 slyar.com/***.html 下
if ($host = 'www.slyar.com' ) {
rewrite ^/(.*)$ http://slyar.com/$1 permanent;
}