很多企业,会申请多个域名,比如com、cn、net、info等,以此来保护企业品牌。这时如果想每个域名在访问时,均自动跳转到com上,要怎样实现呢?
域名准备:web1.com、web2.cn、web3.net、web4.info
1、Apache服务器
首先将域名解析到服务器IP:1.1.1.1
接着在http.conf中添加:
# set .net to .com
ServerName web3.net
ServerAlias www.web3.net
RedirectMatch Permanent ^/(.*) http://web1.com/$1
# set .cn to .com
ServerName web2.cn
ServerAlias www.web2.cn
RedirectMatch Permanent ^/(.*) http://web1.com/$1
# set .info to .com
ServerName web4.info
ServerAlias www.web4.info
RedirectMatch Permanent ^/(.*) http://web1.com/$1
ServerAdmin xxxx@web1.com
ServerName web1.com
ServerAlias www.web1.com
然后重启Apache服务器
2、.htaccess方式
在
RewriteEngine On
RewriteCond %{HTTP_HOST} ^web2.cn [NC,OR]
RewriteCond %{HTTP_HOST} ^web3.net [NC,OR]
RewriteCond %{HTTP_HOST} ^web4.info [NC,OR]
RewriteCond %{HTTP_HOST} ^web1.com$ [NC]
RewriteRule ^(.*)$ http://www.web1.com/$1 [L,R=301]
这里,无论是cn、net还是info,均自动跳转至web1,而且web1.com也将自动跳转到www.web1.com。