You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Apache配置二级域名指向不同目录的实现方案

Hey there! Let's get your subdomain mappings sorted out. Since you didn’t specify which web server you’re using, I’ll cover both Nginx and Apache (the two most common options) with step-by-step configurations tailored to your needs.

First, make sure your DNS records point all three subdomains (www, test, vuelar) to your server’s public IP address—this is a prerequisite before any server config will work.

Nginx Configuration

Create a new config file (e.g., /etc/nginx/sites-available/mydomain.conf) with the following blocks for each subdomain:

www.mydomain.com -> /root/

server {
    listen 80;
    server_name www.mydomain.com;

    root /root/;
    index index.html index.htm; # Adjust to your default index file if needed

    access_log /var/log/nginx/www.mydomain.access.log;
    error_log /var/log/nginx/www.mydomain.error.log;
}

test.mydomain.com -> /root/test

server {
    listen 80;
    server_name test.mydomain.com;

    root /root/test;
    index index.html index.htm;

    access_log /var/log/nginx/test.mydomain.access.log;
    error_log /var/log/nginx/test.mydomain.error.log;
}

vuelar.mydomain.com -> /root/vuelar/public (Laravel Project)

Laravel requires special rewrite rules to route requests through its index.php file, so this block includes those:

server {
    listen 80;
    server_name vuelar.mydomain.com;

    root /root/vuelar/public;
    index index.php index.html index.htm;

    access_log /var/log/nginx/vuelar.mydomain.access.log;
    error_log /var/log/nginx/vuelar.mydomain.error.log;

    # Route all requests to Laravel's index.php
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Handle PHP files (adjust the fastcgi_pass path to match your PHP version)
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Example for PHP 8.1
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # Block access to hidden files like .htaccess
    location ~ /\.ht {
        deny all;
    }
}

Post-Config Steps for Nginx:

  1. Enable the site by creating a symlink:
    sudo ln -s /etc/nginx/sites-available/mydomain.conf /etc/nginx/sites-enabled/
    
  2. Test for config syntax errors:
    sudo nginx -t
    
  3. Restart Nginx to apply changes:
    sudo systemctl restart nginx
    
Apache Configuration

First, enable required modules:

sudo a2enmod rewrite
sudo a2enmod vhost_alias

Create a new config file (e.g., /etc/apache2/sites-available/mydomain.conf) with these virtual host blocks:

www.mydomain.com -> /root/

<VirtualHost *:80>
    ServerName www.mydomain.com
    DocumentRoot /root/

    ErrorLog ${APACHE_LOG_DIR}/www.mydomain.error.log
    CustomLog ${APACHE_LOG_DIR}/www.mydomain.access.log combined

    <Directory /root/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

test.mydomain.com -> /root/test

<VirtualHost *:80>
    ServerName test.mydomain.com
    DocumentRoot /root/test

    ErrorLog ${APACHE_LOG_DIR}/test.mydomain.error.log
    CustomLog ${APACHE_LOG_DIR}/test.mydomain.access.log combined

    <Directory /root/test/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

vuelar.mydomain.com -> /root/vuelar/public (Laravel Project)

This block sets the document root to Laravel’s public folder and includes rewrite rules:

<VirtualHost *:80>
    ServerName vuelar.mydomain.com
    DocumentRoot /root/vuelar/public

    ErrorLog ${APACHE_LOG_DIR}/vuelar.mydomain.error.log
    CustomLog ${APACHE_LOG_DIR}/vuelar.mydomain.access.log combined

    <Directory /root/vuelar/public/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted

        # Laravel rewrite rules (fallback if .htaccess isn't working)
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
    </Directory>
</VirtualHost>

Post-Config Steps for Apache:

  1. Enable the site:
    sudo a2ensite mydomain.conf
    
  2. Restart Apache to apply changes:
    sudo systemctl restart apache2
    
Critical Notes
  • File Permissions: Ensure the web server user (usually www-data for both Nginx and Apache) has read access to your /root directory and its subfolders. Run this to fix permissions if needed:
    sudo chown -R www-data:www-data /root/
    sudo chmod -R 755 /root/
    sudo chmod -R 644 /root/**/*.html /root/**/*.php # Adjust file types as needed
    
  • HTTPS: If you want to use HTTPS, get an SSL certificate (e.g., via Let’s Encrypt) and update the listen directives to 443 ssl, adding paths to your certificate files.
  • Firewall: Make sure ports 80 (HTTP) and 443 (HTTPS) are open on your server firewall. For UFW, run:
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    

内容的提问来源于stack exchange,提问作者Caleb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:36:02