子網域將我重定向到主域 Apache2

子網域將我重定向到主域 Apache2

我有一個在 Apache 伺服器上運行的網站。當我嘗試存取子網域時,我會被重新導向到主網域。

這是 Apache 的設定檔:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin [email protected]
        ServerName azaanjobs.com
        ServerAlias www.azaanjobs.com
        DocumentRoot /var/www/azaanjobs/public_html

        <Directory /var/www/azaanjobs/public_html/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>

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

        Include /etc/letsencrypt/options-ssl-apache.conf

        SSLCertificateFile /etc/letsencrypt/live/azaanjobs.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/azaanjobs.com/privkey.pem
    </VirtualHost>
</IfModule>

<VirtualHost *:8080>
    ServerAdmin [email protected]
    ServerName government-jobs.azaanjobs.com
    ServerAlias www.government-jobs.azaanjobs.com.com
    DocumentRoot /var/www/government-jobs/public_html/
    
    <Directory /var/www/government-jobs/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

我怎樣才能解決這個問題?

答案1

Apache 將嘗試將 Web 請求與配置的網域按照它們在檔案系統中出現的順序進行匹配在設定檔中。作為一般經驗法則,最好在主站點之前處理子域,主站點應配置為充當任何未處理流量的「包羅萬象」。

考慮到這一點,您的設定檔可以更新為如下所示:

<VirtualHost *:8080>
    ServerAdmin [email protected]
    ServerName government-jobs.azaanjobs.com
    ServerAlias www.government-jobs.azaanjobs.com
    DocumentRoot /var/www/government-jobs/public_html/
    
    <Directory /var/www/government-jobs/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin [email protected]
        ServerName azaanjobs.com
        ServerAlias www.azaanjobs.com *.azaanjobs.com
        DocumentRoot /var/www/azaanjobs/public_html

        <Directory /var/www/azaanjobs/public_html/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>

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

        Include /etc/letsencrypt/options-ssl-apache.conf

        SSLCertificateFile /etc/letsencrypt/live/azaanjobs.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/azaanjobs.com/privkey.pem
    </VirtualHost>
</IfModule>

筆記:

  1. 更新的設定檔用正確的引用替換了不正確的www.government-jobs.azaanjobs.com.comServerAlias.com
  2. *.azaanjobs.com主域中新增了別名,以確保捕獲任何「意外」流量並將其路由到可能的位置
  3. 主站點上似乎沒有任何配置*:80,如果伺服器前面的內容沒有將非 SSL 流量轉換為使用 SSL,這可能會出現問題
  4. 該子網域仍在偵聽端口8080,因此需要將其更新為*:80訪問者無需在瀏覽器中指定端口即可查看該站點之前

更改設定檔後記得重新啟動Apache:

sudo service apache2 restart

這應該會給你你所需要的

相關內容