假設有域名domain.com
,已經泛解析子域名*.domain.com
到該主機的ip,web根目錄爲/var/www/
,
在訪問a.domain.com
時,能自動綁定/var/www/a/
目錄,訪問b.domain.com
時,能自動綁定/var/www/b/
目錄html
主機配置文件vhost.conf
linux
以及/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.conf
dom
<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