nginx

 
  
  1. 例: 
  2.     server { 
  3.         listen       80 default
  4.         server_name  www.test.com test.com; 
  5.         location / { 
  6.                 root   /data/webroot/test; 
  7.                 index index.html; 
  8.                 if ($host !~* ^www) { 
  9.                 set $name_www www.$host; 
  10.                 rewrite ^(.*)$ http://$name_www$1 permanent; 
  11.                 } 
  12.         }
  13. } 

 

apache

 
  
  1. <VirtualHost *:80> 
  2.     ServerAdmin webmaster@dummy-host.example.com 
  3.     DocumentRoot "/data/webroot/test" 
  4.     ServerName test.com 
  5.     ServerAlias www.test.com 
  6.     RewriteEngine on 
  7.     RewriteCond %{HTTP_HOST} ^test.com  [NC] 
  8.     RewriteRule ^(.*)$ http://www.test.com$1 [R=permanent,L] 
  9. </VirtualHost>