为什么使用301永久重定向
之前我曾说过,我要更换域名,现在,域名也已经更换完毕。然而,更换域名估计是对网站最不友好的一种方法了。我们建立网站一般都追求流量越大越好,流量越大,说明网站的知名度就越高。然而,一旦更换域名,该网站对于搜索引擎的索引成为一个问题,因为更换域名,新域名没有在搜索引擎的缓存当中,即使你去搜索引擎提交域名,也得几天才能有结果。因此,更换域名后的原域名还要继续使用,不能直接停止解析,如果直接停止解析,会使搜索引擎吃闭门羹,次数多了的话,搜索引擎会认为这个域名已经不再使用,那么,网站流量会越来越少。现在,我的网站流量很小,更换域名也没有很大影响,况且,我还使用了301永久重定向,影响就更小了。
301永久重定向
重定向方法中有临时重定向和永久重定向。临时重定向的返回代码是302,永久重定向的返回代码是301。重定向的写法有很多种,有IIS重定向、Apache重定向及Nginx重定向。我的网站服务器使用的Web软件是Nginx,系统是Linux。因此,我这里贴出的方法仅是Nginx的301写法。其他的写法请找度娘,或是Google。首先我们要进入Nginx所在的目录,目录一般是:/usr/local/nginx,可以在命令行中直接用cd /usr/local/nginx,然后在进入 conf目录下的 vhost目录,在这个目录中可以找到 域名.conf文件,在使用 vi 域名.conf进行编辑。我现在贴出一份正常的Nginx代码。
server { listen 80; #listen [::]:80; server_name www.poipod.com poipod.com *.poipod.com; index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/www.poipod.com; include wordpress.conf; #error_page 404 /404.html; # Deny access to PHP files in specific directory #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; } include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; "www.poipod.com.conf" 37L, 844C
上面的代码是正常的Nginx代码,如果需要重定向,只需在 server_name后面另起一行,写上代码:
if ($host != 'www.podipod.com' ) { rewrite ^/(.*)$ http://www.podipod.com/$1 permanent; }
当然,域名请自行修改,否则解析到我的域名上面,概不负责。完整的代码如下
server { listen 80; #listen [::]:80; server_name www.oldpodipod.com *.oldpodipod.com olpodipod.com; if ($host != 'www.podipod.com' ) { rewrite ^/(.*)$ http://www.podipod.com/$1 permanent; } index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/www.olpodipod.com; include wordpress.conf; #error_page 404 /404.html; # Deny access to PHP files in specific directory #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; } "www.oldpodipod.com.conf" 45L, 1329C
本文由 podipod软库网 作者:DevOps 发表,转载请注明来源!