用Apache重写规则配置虚拟二级域名

Standard

作用:
在用户注册后提供类似于 username.yemaosheng.com 之类的二级域名服务
步骤:
首先要去你注册域名的网站的域名管理面板中配置你的域名为泛域名解析(不是每个域名服务商都提供泛域名解析。我的域名是万网注册的,有这个服务)。
具体配置示例如下(图):
A记录域名解析
比如你有个叫yemaosheng.com的域名,则,在你的域名管理面板中添加一条A记录,记录的RR值配置为通配符(星号*),这样,完整的记录就是 *.yemaosheng.com ,指向你的服务器所在的IP。
然后对服务器上的Apache配置如下(注:QQ空间将反斜杠显示为”\”)

    NamevirtualHost *:80
    <VirtualHost *:80>
     ServerAdmin webmaster@yemaosheng.com
     DocumentRoot /var/www/yemaosheng
     ServerName *.yemaosheng.com
     RewriteEngine on
     RewriteCond %{HTTP_HOST} ^[a-zA-Z0-9]+\.yemaosheng\.com$
     RewriteRule ^/?$ /%{HTTP_HOST}
     RewriteRule ^/www\.yemaosheng\.com/?$ /
     RewriteRule ^/news\.yemaosheng\.com/?$ /news.php
     RewriteRule ^/bbs\.yemaosheng\.com/?$ /bbs.php
     RewriteRule ^/blog\.yemaosheng\.com/?$ /blog.php
     RewriteRule ^/([a-zA-Z0-9]+)\.yemaosheng\.com/?$ /blog.php?user=$1
     <Directory /var/www/yemaosheng>
       Options None
       AllowOverride All
       Order allow,deny
       Allow from all
     </Directory>
    </VirtualHost>

One thought on “用Apache重写规则配置虚拟二级域名

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.