Sivo 悉见

nginx 不带www到www域名的301永久重定向

匿名 · 更新于 2014/4/8

由于网站需要,把不带www和带www的域名合并到一起,而新网的又没有了转向功能,只能由服务器下手。谷歌知道需要使用301定向。以便phodal.com转向www.phodal.com

原来的配置文件如下
 

server {
        listen 80;
        server_name phodal.com www.phodal.com; 
	index index.html index.htm index.php;
	root /alidata/www/phodal;
	#下方注释掉 包括{,}
	#location ~ .*\.(php|php5)?$
	#{
		#fastcgi_pass  unix:/tmp/php-cgi.sock;
		#fastcgi_pass  127.0.0.1:9000;
		#fastcgi_index index.php;
		#include fastcgi.conf;
	#}
location ~ \.php {
	fastcgi_pass 127.0.0.1:9000;
	fastcgi_index index.php;
	include fcgi.conf;
	set $real_script_name $fastcgi_script_name;
	if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
		set $real_script_name $1;
		set $path_info $2;
	}
	fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
	fastcgi_param SCRIPT_NAME $real_script_name;
	fastcgi_param PATH_INFO $path_info;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
	expires 30d;
}
location ~ .*\.(js|css)?$
{
	expires 1h;
}

location / {        
	if (!-e $request_filename) {
		rewrite  ^/(.*)$  /index.php/$1  last;
				break;
	}
}
#伪静态规则1
#include /alidata/server/nginx/conf/rewrite/phpwind.conf;
access_log  /alidata/log/nginx/access/phpwind.log;
error_page 404 = /Phodal/Tpl/Public/404.html;

}


 将phodal.com删除再添加
 

server {
    server_name phodal.com;
    rewrite ^(.*) http://www.phodal.com$1 permanent;//permanent设置永久重定向
}

最终代码如下

server {
        listen       80;
        server_name  www.phodal.com;
	index index.html index.htm index.php;
	root /alidata/www/phodal;
	#下方注释掉 包括{,}
	#location ~ .*\.(php|php5)?$
	#{
		#fastcgi_pass  unix:/tmp/php-cgi.sock;
		#fastcgi_pass  127.0.0.1:9000;
		#fastcgi_index index.php;
		#include fastcgi.conf;
	#}
location ~ \.php {
	fastcgi_pass 127.0.0.1:9000;
	fastcgi_index index.php;
	include fcgi.conf;
	set $real_script_name $fastcgi_script_name;
	if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
		set $real_script_name $1;
		set $path_info $2;
	}
	fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
	fastcgi_param SCRIPT_NAME $real_script_name;
	fastcgi_param PATH_INFO $path_info;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
	expires 30d;
}
location ~ .*\.(js|css)?$
{
	expires 1h;
}

location / {        
	if (!-e $request_filename) {
		rewrite  ^/(.*)$  /index.php/$1  last;
				break;
	}
}
#伪静态规则1
#include /alidata/server/nginx/conf/rewrite/phpwind.conf;
access_log  /alidata/log/nginx/access/phpwind.log;
error_page 404 = /Phodal/Tpl/Public/404.html;

} server { server_name phodal.com; rewrite ^(.*) http://www.phodal.com$1 permanent; }