WenRou's Blog
nginx 301 域名重定向
2015-3-17 温柔哥


以下是以discuz论坛为例



server

{

listen 80;

server_name wenrouge.com ;



if ($host != 'www.wenrouge.com' ) {

rewrite ^/(.*)$ http://www.wenrouge.com/$1 permanent;

}



index index.html index.htm index.php default.html default.htm default.php;

root /home/wwwroot;



include discuz.conf;

location ~ .*\.(php|php5)?$

{

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 off;

}



注意:关键的地方是


server_name wenrouge.com ;
if ($host != 'www.wenrouge.com' ) {
rewrite ^/(.*)$ https://www.wenrouge.com/$1 permanent;
}

该代码的意思 输入的域名如果不是wenrouge.com就直接跳转到www.wenrouge.com
也可以这样写
server_name wenrouge.com ;
if ($host = 'wenrouge.com' ) {
rewrite ^/(.*)$ https://www.wenrouge.com/$1 permanent;
}

该代码的意思 输入的域名如果为 wenrouge.com就直接跳转到www.wenrouge.com




发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容