LNMP下实现域名301重定向的办法

我装的是LNMP,是Nginx环境,怎么用301重定向把带www和不带www的网址合并,因为实现的方法是:

打开/usr/local/nginx/conf/vhost下相应的.conf文件

原代码如下:

server
{
listen 80;
server_name www.zhangwenbin.com.cn zhangwenbin.com.cn;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.zhangwenbin.com.cn;
include none.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 www.zhangwenbin.com.cn zhangwenbin.com.cn; 的zhangwenbin.com.cn删除掉,

然后在代码的最下面再加上一个server段:
server {
server_name zhangwenbin.com.cn;
rewrite ^(.*)$ http://www.zhangwenbin.com.cn$1 permanent;
}

最后得到的完整代码是:
server
{
listen 80;
server_name www.zhangwenbin.com.cn;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.zhangwenbin.com.cn;
include none.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 {
server_name zhangwenbin.com.cn;
rewrite ^(.*)$ http://www.zhangwenbin.com.cn$1 permanent;
}

这样用户打开zhangwenbin.com.cn时候就会转到www.zhangwenbin.com.cn去了,注意,zhangwenbin.com.cn虽然用了301重定向,但还是要做A记录解析。

发表评论?

3 条评论。

  1. if ($host = ‘startcn.net’ ) {
    rewrite ^/(.*)$ http://www.startcn.net/$1 permanent;
    }

  2. 这个域名url跳转能实现301的效果吧?

  3. 可以,是301重定向

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>