阳光越强烈
阴影就越是深邃

域名301重定向

一般我们域名解析后有两个子域名:比如www.taiyangta.comwww.taiyangta.com,虽然多了个域名,可以解析到其他网站,按照习惯,一般人访问都是加www,但是搜索引擎却把这两个域名指向的网站认为是两个单独的网站,这对于SEO很不利,所以才想着把www.taiyangta.com重定向到www.taiyangta.com。首先是看你的主机的web环境,下面给出linux下apachenginx301重定向方法:

Apache

要求空间开启了mod_rewrite功能,不适用于windows主机。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.taiyangta.com$ [NC]
RewriteRule ^(.*)$ www.taiyangta.com/$1 [R=301,L]
</IfModule>

把上面代码复制,建立一个无文件名的.htaccess文件,上传到网站的根目录下就可以了!假如你的网站有伪静态,已经存在这个文件,那么可以把下面的

RewriteCond %{HTTP_HOST} ^www.taiyangta.com$ [NC]
RewriteRule ^(.*)$ www.taiyangta.com/$1 [R=301,L]

加入到之前的.htaccess文件中,比如之前是wordpress的伪静态规则,那么可以修改文件为:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.taiyangta.com$ [NC]
RewriteRule ^(.*)$ www.taiyangta.com/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

这样就即可301重定向,又可以开启博客伪静态了!

域名301重定向这个规则必须放在静态支持规则的前面,不然可能无法生效。

NGNIX

假如我们以www.taiyangta.com为主域名,解决的办法如下:
怎么用301重定向把带www和不带www的网址合并,使.www.taiyangta.com重定向,
实现的方法是:
打开/usr/local/nginx/conf/vhost下相应的www.taiyangta.com.conf文件,原代码如下:

server
{
listen 80;
server_name wwwwww.taiyangta.com www.taiyangta.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.taiyangta.com; 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.taiyangta.com www.taiyangta.com; 的www.taiyangta.com删除掉然后在代码的最下面再加上一个server段:

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

最后得到的完整代码是:

server

{

listen 80;

server_name www.taiyangta.com;

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

root /home/wwwroot/www.taiyangta.com; 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_namewww.taiyangta.com;

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



}

修改完以后,别忘了重启一下nginx服务:#/root/lnmp restart 

301重定向主要是修改nginx的核心变量,目标网站的conf文件server函数。 

赞(0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址