sanhutrees 发表于 2018-11-20 11:50:17

Apache配置301跳转

  我们现在有test和aaa在访问这个discuz论坛,那么这两个域名坑定会分一个主次,尤其是现在搜索引擎他会有一个排名权重,那么官方语言叫做“PR",那么PR高低会有影响的,那搜索引擎分辨不出主次,所以我们现在帮搜索引擎分清主次,这个时候我们就有一种方法叫做跳转:域名的跳转,访问aaa.com跳转到test.com,那我配置一下编辑虚拟主机配置文件
  # vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
  配置跳转,跳转用到一个模块叫做Rewrite模块
  
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^www.aaa.com$
  RewriteRule ^/(.*)$http://www.test.com/$1
  
  我们来看一下Rewrite模块,有没有加载
  # apachectl -M
  Loaded Modules:
  core_module (static)
  authn_file_module (static)
  authn_default_module (static)
  authz_host_module (static)
  authz_groupfile_module (static)
  authz_user_module (static)
  authz_default_module (static)
  auth_basic_module (static)
  include_module (static)
  filter_module (static)
  log_config_module (static)
  env_module (static)
  setenvif_module (static)
  version_module (static)
  mpm_prefork_module (static)
  http_module (static)
  mime_module (static)
  status_module (static)
  autoindex_module (static)
  asis_module (static)
  cgi_module (static)
  negotiation_module (static)
  dir_module (static)
  actions_module (static)
  userdir_module (static)
  alias_module (static)
  so_module (static)
  deflate_module (shared)
  expires_module (shared)
  rewrite_module (shared)共享模块
  php5_module (shared)
  Syntax OK
  我们需要改一下配置:
  

RewriteCond %{HTTP_HOST} ^www.aaa.com$ 跳转条件
RewriteRule ^(.*)$http://www.test.com/$1 规则 L:表示LAST结束
^:以。。。。。开头
/(.*)是www.test.com/后面的 www.test.com代表HTTP_HOST
用$1去标记 最终会跳转到这个www.test.com 的$1
R=301 这个301是状态码
跳转分两种:一种是301 永久重定向,对搜索引擎友好
            一种是302 临时重定向
我们网页域名跳转一定要用301,对搜索引擎非常友好的
可以用curl测试
  # curl -x 192.168.140.100:80www.aaa.com -I
  HTTP/1.1 301 Moved Permanently
  Date: Mon, 28 Dec 2015 15:56:11 GMT
  Server: Apache/2.2.31 (Unix) PHP/5.3.27
  Location: http://www.test.com//
  Content-Type: text/html; charset=iso-8859-1
  测试的时候可以指定一个IP ,-I可以看到状态码
  有时候我们也会遇到不止两个域名的情况,aaa或者是bbb
  
  DocumentRoot "/data/www/"
  ServerNamewww.test.com
  ServerAlias www.aaa.com
  ServerAlias www.bbb.com
  # ErrorLog "logs/dummy-host2.example.com-error_log"
  # CustomLog "logs/dummy-host2.example.com-access_log" common
  
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^www.aaa.com$
  RewriteCond %{HTTP_HOST} ^www.bbb.com$
  RewriteRule ^/(.*)$http://www.test.com/$1
  
表示或者的意思,不加认为这两个条件同时存在
  




页: [1]
查看完整版本: Apache配置301跳转