apache 子域名自動與子域名同名的目錄綁定

假設有域名domain.com,已經泛解析子域名*.domain.com到該主機的ip,web根目錄爲/var/www/
在訪問a.domain.com時,能自動綁定/var/www/a/目錄,訪問b.domain.com時,能自動綁定/var/www/b/目錄html

 

一、分別手動創建相應的virtualhost

主機配置文件vhost.conflinux

<VirutualHost *:80>
    DocumentRoot /var/www/
    ServerName domain.com
    ServerAlias *.domain.com
</VirtualHost>

以及/var/www/.htaccess文件web

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
    RewriteCond %{REQUEST_URL} !^/%1/
    RewriteRule ^(.*)$ %1/$1
</IfModule>

此時不論以哪一個域名訪問,都會返回服務器內部錯誤apache

若是把RewriteCond %{REQUEST_URL} !^/%1/這行配置中的%1替換成具體的字符串,如a,這樣的話,可以實現a.domain.com綁定到/var/www/a/目錄,可是其餘域名依然是/var/www/目錄服務器

 

二、任意主機名的虛擬主機

RewriteEngine on

RewriteMap    lowercase int:tolower

RewriteCond   "${lowercase:%{HTTP_HOST}}"   "^www\.([^.]+)\.example\.com$"
RewriteRule   "^(.*)" "/home/%1/www$1"

 

 

三、vhost_alias模塊實現通配子域名

主機配置文件vhost.confdom

<VirtualHost *:80>
ServerAdmin webmaster@localhost
 
ServerName test.loc
ServerAlias *.test.loc
 
# This will be the wildcarded document root. Any folder you create in /var/www/test will be automatically a subdomain name.
VirtualDocumentRoot /var/www/test/%-3
 
# For symlinking your projects and htaccesses to work
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
 
ErrorLog /var/log/apache2/vhosts-error.log
 
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
 
CustomLog /var/log/apache2/vhosts-access.log combined
</VirtualHost>

https://inchoo.net/magento/how-to-setup-semiautomatic-per-folder-wildcard-subdomains-on-your-local-linux-based-development-computer/ide

https://httpd.apache.org/docs/2.4/rewrite/vhosts.htmlspa

Apache多虛擬主機多版本PHP(5.2+5.3+5.4)共存運行配置全過程 .net

相關文章
相關標籤/搜索