apache实现动态二级域名
apache实现动态子域名,abc.zblog.com,则访问/data/www/default/zblog/abc
1.查找httpd.conf所在的位置
root@instance-baidu:/usr/local/apache/conf# sudo find / -name httpd.conf
/usr/local/apache/conf/httpd.conf
2.修改httpd.conf
vi /usr/local/apache/conf/httpd.conf
#取消注释这行
LoadModule rewrite_module modules/mod_rewrite.so
3.修改虚拟主机的配置(如何配置虚拟主机)
sudo vi /usr/local/apache/conf/vhost/default.conf
<VirtualHost *:80> DocumentRoot /data/www/default/zblog ServerName zblog.com Rewriteengine On
RewriteCond %{HTTP_HOST} !^www\.zblog\.com$ //这行是避免www也被重写 RewriteCond %{HTTP_HOST} ^[^.]+\.zblog\.com$ RewriteRule ^(.+) %{HTTP_HOST}$1 [C] RewriteRule ^([^.]+)\.zblog\.com(.*) /$1$2 SetOutputFilter DEFLATE Options FollowSymLinks DirectoryIndex index.php index.html index.htm </VirtualHost>
重启一下服务 service httpd restart
4.修改hosts文件做测试:
IP改为你自己的服务器IP
sudo vi /etc/hosts 127.111.39.58 abc.zblog.com 127.111.39.58 zblog.com
abc文件夹的内容是:
root@instance-baidu:/data/www/default/zblog/abc# pwd /data/www/default/zblog/abc root@instance-baidu:/data/www/default/zblog/abc# cat index.html i am abc page.
特殊域名可以设置代理或其它重写规则:访问baidu.zblog.com代理到http://www.baidu.com
<VirtualHost *:80>
DocumentRoot /data/www/default/zblog
ServerName zblog.com
Rewriteengine On
RewriteCond %{HTTP_HOST} !^baidu\.zblog\.com$
RewriteCond %{HTTP_HOST} !^www\.zblog\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.zblog\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.zblog\.com(.*) /$1$2
RewriteCond %{HTTP_HOST} ^baidu\.zblog\.com$
RewriteRule ^/(.*) http://www.baidu.com [P]
LogLevel warn rewrite:trace3
SetOutputFilter DEFLATE
Options FollowSymLinks
DirectoryIndex index.php index.html index.htm
</VirtualHost>
查看apache日志:
tail -f /usr/local/apache/logs/error_log
参考:
https://blog.csdn.net/mzllyl/article/details/84434976
详细的rewrite规则,参考: