进入到Apache的配置文件:cd /etc/httpd/conf/http.conf
在后面添加:
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot "/var/www/html/"
</VirtualHost>
详细参考官网:http://httpd.apache.org/docs/2.4/vhosts/name-based.html
附:
<IfModule rewrite_module>
RewriteEngine on
RewriteRule "^/introduction/(.*)" "http://localhost:9005/introduction/$1" [R,L,P]
</IfModule>
将请求为introducton开头的转到http://localhost:9005/introduction请求
若虚拟主机中请求localhost,就要将两者相结合:
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot "/var/www/html/catalogs"
<IfModule rewrite_module>
RewriteEngine on
RewriteRule "^/introduction/(.*)" "http://localhost:9005/introduction/$1" [R,L,P]
</IfModule>
</VirtualHost>
拓展:不带WWW的域名跳转到带WWW的域名地址Apache重写规则
第一步:Apache虚拟机配置:
<VirtualHost *:80>
ServerAdmin mac@xobm.com
DocumentRoot “/var/www/www.xobm.com/”
ServerName www.xobm.com
ServerAlias xobm.com //这句是关键,配置别名
ErrorLog “logs/dummy-host2.xobm.com-error.log”
CustomLog “logs/dummy-host2.xobm.com-access.log” common
<Directory />
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
第二步:HTACCESS写法
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^xobm.com [NC]
RewriteRule ^(.*) http://www.xobm.com/ [L]
</IfModule>