说下需求:公司要做中英文两个网站,所有用户访问www.a.com 按国家来判断,
中国用户访问则跳转到www.a.com/cn 目录下面 该目录是中文网站;
国外用户访问则跳转到www.a.com/en 目录下面 该目录是英文网站;
实现方法:通过apache的geoip模块来实现,该模块有基于用户ip来判断用户属于哪个国家mod_geoip.so库,然后通过Apache rewrite规则来跳转到对应的目录下,实现公司需求。
服务器系统:centos6.5 64位
我们用的是阿里云的服务器,阿里帮我们集成了epel源
1、安装geoip模块
yum -y install mod_geoip
2、下载最新的geoip国家库
yum安装的mod_geoip库比较老,去Geoip的官网下载最新GeoLite Country库并替换原有的GeoIP.dat,下载地址:http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz,解压替换:
gzip -d GeoIP.dat.gz
mv GeoIP.dat /usr/share/GeoIP/GeoIP.dat
3、配置 mod_geoip 跳转
#汉语版跳转到汉语主页 RewriteEngine on RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CN$ RewriteRule ^/$ /cn/index.html [R=301,L] #英语版跳转到英语主页 RewriteEngine on RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^US$ RewriteRule ^/$ /en/index.html [R=301,L]
4、保存后重启apache服务
/etc/init.d/httpd restart
用美国vps测试下:
curl www.a.com
<title>301 Moved Permanently</title> </head><body> <h1>Moved Permanently</h1> <p>The document has moved <a href="http://www.a.com/en/index.html">here</a>.</p> <hr>
看到301跳转,ok测试通过
转载于:https://blog.51cto.com/gjr0512/1749830