Apache2配置实现子域名匹配Apache2配置目录下的子目录
sudo a2enmod rewrite
nano /etc/apache2/sites-available/000-default.conf
sudo systemctl restart apache2
<VirtualHost *:80>
DocumentRoot /var/www/html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(\w+)\.gazi\.asia$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
第二中自己手动匹配目录
<VirtualHost *:80>
DocumentRoot /var/www/html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.gazi\.asia$ [NC]
RewriteRule ^(.*)$ /sub/$1 [L]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
第三次域名匹配
RewriteCond %{HTTP_HOST} ^(\w([-_a-zA-Z0-9])+).gazi.asia$ [NC]
第四次配置,保证匹配更全面
<VirtualHost *:80>
DocumentRoot /var/www/html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(([-_a-zA-Z0-9])+)\.\w+\.\w+$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ErrorDocument 404 "404 Not Found"
</VirtualHost>