LAMP搭建8:设置301域名跳转
2017-01-11 16:00
197 查看
现在我们有两个域名都能访问我们的论坛:www.test.com和www.mydiscuz.com
对于浏览器排名pr来说,这是不利的,我们需要设置其中一个为主域名,然后设置其他从域名到主域名的301跳转,这样我们的主域名在搜索引擎中的排名就会上升。
编辑虚拟主机配置文件
[root@CentOS6 important]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
添加一个rewrite模块实现301跳转,内容如下:
<VirtualHost *:80>
DocumentRoot "/tmp/123"
ServerName invalid.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/www"
ServerName www.test.com
ServerAlias www.mydiscuz.com
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mydiscuz.com$
RewriteRule !/(.*)$ http://www.test.com/$1 [R=301,L]
</IfModule>
<Directory /data/www/important/>
AllowOverride AuthConfig
AuthName "username"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</Directory>
301表示永久重定向,域名跳转用这个
302表示临时重定向
L 表示结束
检查无误后重新加载配置文件:
[root@CentOS6 important]# apachectl -t
Syntax OK
[root@CentOS6 important]# apachectl graceful
使用www.mydiscuz.com域名访问时
![](https://s3.51cto.com/wyfs02/M02/8C/B9/wKioL1h15gLAHD6lAAA3ayB4AZ8411.png-wh_500x0-wm_3-wmp_4-s_1910300394.png)
回车后自动跳转到www.test.com域名
![](https://s3.51cto.com/wyfs02/M01/8C/BC/wKiom1h15gOwH1TxAAAZkpVCK8o884.png-wh_500x0-wm_3-wmp_4-s_1095180294.png)
也可以使用curl命令查看跳转
[root@CentOS6 important]# curl -x192.168.147.131:80 www.mydiscuz.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.test.com/">here</a>.</p>
</body></html>
[root@CentOS6 important]#
不止两个域名的情况,将多个从域名跳转到主域名:编辑虚拟主机配置文件,再加入一个从域名www.others.com,并配置让它也301跳转到主域名www.test.com:
<VirtualHost *:80>
DocumentRoot "/tmp/123"
ServerName invalid.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/www"
ServerName www.test.com
ServerAlias www.mydiscuz.com
ServerAlias www.others.com
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mydiscuz.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.others.com$
RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
</IfModule>
<Directory /data/www/important/>
AllowOverride AuthConfig
AuthName "username"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</Directory>
</VirtualHost>
测试跳转:
[root@CentOS6 important]# apachectl -t
Syntax OK
[root@CentOS6 important]# apachectl graceful
[root@CentOS6 important]# curl -x192.168.147.131:80 www.others.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.test.com/">here</a>.</p>
</body></html>
[root@CentOS6 important]#
对于浏览器排名pr来说,这是不利的,我们需要设置其中一个为主域名,然后设置其他从域名到主域名的301跳转,这样我们的主域名在搜索引擎中的排名就会上升。
编辑虚拟主机配置文件
[root@CentOS6 important]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
添加一个rewrite模块实现301跳转,内容如下:
<VirtualHost *:80>
DocumentRoot "/tmp/123"
ServerName invalid.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/www"
ServerName www.test.com
ServerAlias www.mydiscuz.com
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mydiscuz.com$
RewriteRule !/(.*)$ http://www.test.com/$1 [R=301,L]
</IfModule>
<Directory /data/www/important/>
AllowOverride AuthConfig
AuthName "username"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</Directory>
301表示永久重定向,域名跳转用这个
302表示临时重定向
L 表示结束
检查无误后重新加载配置文件:
[root@CentOS6 important]# apachectl -t
Syntax OK
[root@CentOS6 important]# apachectl graceful
使用www.mydiscuz.com域名访问时
![](https://s3.51cto.com/wyfs02/M02/8C/B9/wKioL1h15gLAHD6lAAA3ayB4AZ8411.png-wh_500x0-wm_3-wmp_4-s_1910300394.png)
回车后自动跳转到www.test.com域名
![](https://s3.51cto.com/wyfs02/M01/8C/BC/wKiom1h15gOwH1TxAAAZkpVCK8o884.png-wh_500x0-wm_3-wmp_4-s_1095180294.png)
也可以使用curl命令查看跳转
[root@CentOS6 important]# curl -x192.168.147.131:80 www.mydiscuz.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.test.com/">here</a>.</p>
</body></html>
[root@CentOS6 important]#
不止两个域名的情况,将多个从域名跳转到主域名:编辑虚拟主机配置文件,再加入一个从域名www.others.com,并配置让它也301跳转到主域名www.test.com:
<VirtualHost *:80>
DocumentRoot "/tmp/123"
ServerName invalid.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/www"
ServerName www.test.com
ServerAlias www.mydiscuz.com
ServerAlias www.others.com
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mydiscuz.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.others.com$
RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
</IfModule>
<Directory /data/www/important/>
AllowOverride AuthConfig
AuthName "username"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</Directory>
</VirtualHost>
测试跳转:
[root@CentOS6 important]# apachectl -t
Syntax OK
[root@CentOS6 important]# apachectl graceful
[root@CentOS6 important]# curl -x192.168.147.131:80 www.others.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.test.com/">here</a>.</p>
</body></html>
[root@CentOS6 important]#
相关文章推荐
- 域名跳转301-LAMP环境搭建
- 2015-12-17LAMP第二部分apache配置1(discuz搭建论坛 and 域名跳转 and 用户验证)
- LAMP 1.9域名301跳转
- Apache的Rewrite设置多域名301跳转到主域名的方法
- nginx域名301跳转设置、服务器IP地址禁止访问500设置
- LAMP--域名 301 跳转
- javascript,HTML,PHP,ASP做301跳转代码 SEO优化设置
- nginx实现301域名跳转
- .htaccess中301强制跳转到带www前缀或不带www的域名
- 如何实现301的跳转?当输入域名http://xxx.com的时候自动重定向到www上去
- Windows2003 IIS上设置301定向,实现不带www域名跳转带www域名的方法
- centos7 lamp环境配置及多域名设置
- LAMP_ 配置域名跳转
- 通过修改.htaccess文件设置域名的别名自动跳转
- 域名跳转301
- 搭建LAMP环境,并实践基于DNS做基于域名的虚拟主机
- ubuntu LAMP搭建 多域名绑定
- Apache设置www域名跳转至非www域名
- godaddy域名转发(域名跳转)设置教程
- nginx 301 域名跳转