使用 Apache VirtualHost 创建通配符子域 [英] Creating Wildcard Sub Domain Using Apache VirtualHost

查看:32
本文介绍了使用 Apache VirtualHost 创建通配符子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这种情况:

  1. 如果用户请求使用此 URL:example.comwww.example.com,用户会在这个目录/home/admin1/public_html/

  1. if user request using this URL : example.com or www.example.com, user will see index.php in this directory /home/admin1/public_html/

但是当用户请求使用其他子域(通配符)例如:freediscount.example.com 时,用户将在此路径中看到 index.php :/home/admin1/public_html/userweb/freediscount.example.com

but when user request using other sub domain (wildcard) for example : freediscount.example.com, user will see index.php in this path : /home/admin1/public_html/userweb/freediscount.example.com

我的主机的技术支持建议我使用这种方法:http://www.wiredstudios.com/php-programming/setting-up-wildcard-dns-for-subdomains-on-cpanel.html

technical support on my hosting suggest me to use this method : http://www.wiredstudios.com/php-programming/setting-up-wildcard-dns-for-subdomains-on-cpanel.html

根据该教程,PHP 有一项新工作......当用户请求子域时重定向特定文件夹.我不喜欢这种方法.对我来说,如果 Apache 可以处理这个会更好.

based on that tutorial, the PHP has a new job... to redirect on specific folder when user request with sub domain. I don't like this method. for me, it would be better if Apache can handle this.

几乎接近我需要的是这种方法:Virtualhost For Wildcard Subdomain and静态子域

nearly close to what I need is this method : Virtualhost For Wildcard Subdomain and Static Subdomain

但是,我对 VirtualHost 设置有问题,如何在这种情况下正确创建 VirtualHost?

but, I have a problem with VirtualHost setting, how to create VirtualHost correctly for that situation?

这是我做过但没有用的:

here's what I've done but didn't work :

## I think this one is for www or without www, automatically generated with WHM
<VirtualHost xx.xx.xx.xx:80> 
ServerName example.com
ServerAlias www.example.com
DocumentRoot /home/admin1/public_html
</VirtualHost>

## Here's what I'm trying to add
<VirtualHost xx.xx.xx.xx:80>
    ServerName example.com
    DocumentRoot /home/admin1/public_html/userweb/*
</VirtualHost>

推荐答案

使用 Apache 虚拟主机绝对可以使用通配符子域.

Wildcard sub-domains are definitely possible using Apache virtual hosts.

我有基本相同的要求,并设法让它与 Apache 的 mod_vhost_alias.so 模块一起工作.在您的 http-vhosts.conf 文件中试试这个:

I had basically the same requirements and managed to get it working with Apache's mod_vhost_alias.so module. Try this in your http-vhosts.conf file:

DocumentRoot "/home/admin1/public_html/userweb/" 
<Directory "/home/admin1/public_html/userweb/"> 
    Options None 
    AllowOverride None 
    Order allow,deny 
    Allow from all 
</Directory>

<VirtualHost *:80>
    DocumentRoot /home/admin1/public_html/
    ServerName www.example.com
</VirtualHost>

<VirtualHost *:80> 
    VirtualDocumentRoot /home/admin1/public_html/userweb/%1.example.com/ 
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /home/admin1/public_html/
    ServerName example.com
</VirtualHost>

请注意,我尚未对此进行测试,但它与对我有用的解决方案非常接近.

Note that I haven't tested this, but it's pretty close to the solution that worked for me.

我的解决方案的完整细节在这里:http://www.calcatraz.com/blog/wildcard-subdomains-in-apache-1422

Full details of my solution are here: http://www.calcatraz.com/blog/wildcard-subdomains-in-apache-1422

这篇关于使用 Apache VirtualHost 创建通配符子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆