在LNMP服务器上301重定向域名最简单的方法

假设你的LNMP服务器已经搭建好,且域名www.aaa.com ,www.bbb.com都已经绑定到本服务器,

现在想把www.aaa.com域名301重定向到www.bbb.com

先在/usr/local/nginx/conf/vhost找到www.aaa.com.conf ,用vi打开显示如下:

server
{
listen 80;
server_name www.aaa.com;
index index.html index.htm index.php default.html default.htm de fault.php;
root /home/wwwroot/www.aaa.com;

include none.conf;
#error_page 404 /404.html;
location ~ [^/]\.php(/|)
{
# comment try_files
uri =404; to enable pathinf o
try_files uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
#include pathinfo.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

access_log off;
}

在编辑模式下把server_name www.aaa.com;这行删除,

同时加入

server {
server_name www.aaa.com;
return 301 scheme://www.bbb.comrequest_uri;
}

就可以了,最终修改后的文件如下:

server
{
listen 80;
index index.html index.htm index.php default.html default.htm de fault.php;
root /home/wwwroot/www.aaa.com;

include none.conf;
#error_page 404 /404.html;
location ~ [^/]\.php(/|)
{
# comment try_files
uri =404; to enable pathinf o
try_files uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
#include pathinfo.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)
{
expires 30d;
}

location ~ .*\.(js|css)?{
expires 12h;
}

access_log off;
}

server {
server_name www.aaa.com;
return 301scheme://www.bbb.com$request_uri;
}

发表回复