正文
Apache22中配置虚拟主机(Apache VirtualHost)
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
Apache VirtualHost的作用就是可以让一个apache为多个域名服务,相当于一个服务器挂了N多个网站,举个例子:
我的apache服务器,ip为x.x.x.x,我有两个域名www.tootoogo.org和lp.tootoogo.org,设置这两个域名都指向x.x.x.x,在apache服务器上设置virtual host后,apache服务器可以实现同时为这两个域名服务,使得这两个域名分别指向不同的网页,于是我的一个服务器上就同时有了两个网站。
Apache VirtualHost配置方法:
在httpd.conf文件末尾添加以下内容
NameVirtualHost *:80
<VirtualHost *:80>
ServerAlias www.tootoogo.org #貌似这一行没必要
DirectoryIndex index.html index.php
DocumentRoot "/var/www/data1/“
ServerName www.tootoogo.org
ErrorLog "log/tootoogo-error_log"
CustomLog "logs/tootoogo.access"
</VirtualHost>
<VirtualHost *:80>
DirectoryIndex index.html index.php
DocumentRoot /var/www/data2/
ServerName lp.tootoogo.org
ErrorLog "log/lp.tootoogo-error_log"
CustomLog "logs/lp.tootoogo.access"
</VirtualHost>
这样就可以使用两个域名解析到同一IP上却得到不同页面。
如果要禁止用户直接用IP进行访问,只要在
NameVirtualHost *:80
这句之后插入以下内容:
<VirtualHost *:80>
ServerName YourIpAddress
<Location />
Order Allow,Deny
Deny from all
</Location>
</VirtualHost>
备注:
修改httpd.conf中的
<Directory "/usr/local/www/apache22/data">
为
<Directory "/usr/local/www/apache22/">
那么就可以
使用下面的目录分别放置这两个域名对应的网页了
DocumentRoot /usr/local/www/apache22/data1
DocumentRoot /usr/local/www/apache22/data1
////////////////题外话////////////
如果需要监听其他端口号,可以加入
Listen yourIP:port,例如Listen 192.168.1.1:8081,那么对应的
NameVIrtualHost *:8081<VirtualHost *:8081>
ServerName www.tootoogo.org:8081
//////////////////////////////////
重写例子
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2&%1 RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3&%1 RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3&%1 RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page\%3D$4&page=$3&%1 RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3&%1 RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3&%1 RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3&%1 RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3&%1 RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3&%1 </IfModule>