將所有子網域重定向到 vhost 內的主網域

將所有子網域重定向到 vhost 內的主網域

我只是想重定向虛擬主機中尚未提及的所有子網域ServerName以重定向到空子網域。我嘗試將此網域的所有其他虛擬主機新增到我的 httpd.conf 中。

<VirtualHost *:80>
    ServerName *.example.com
    RedirectPermanent / http://example.com/
</VirtualHost>

空子網域(之前載入)的部分如下所示。

<VirtualHost *:80>
    DocumentRoot /var/www/example/htdocs
    ServerName example.com
    <Directory /var/www/example/htdocs>
        Options FollowSymLinks
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

重新啟動 httpd 服務後,將瀏覽器指向 403 Forbiddenabc.example.com.我究竟做錯了什麼?我希望不需要如上所述的基於正規表示式的匹配在其他答案中對於這個簡單的任務。

答案1

只需在主塊下方添加一個塊即可虛擬主機設定檔。只需指定對子網域ServerAlias使用通配符即可。*最後,使用 指定重定向位址RedirectPermanent

Listen 80
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ServerName example.com

        <Directory /var/www/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

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

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerAlias *.example.com
    RedirectPermanent / http://example.com/
</VirtualHost>

答案2

<VirtualHost *:80>
    DocumentRoot "/var/www/example"
    ServerName *.example.org
    RedirectPermanent / http://example.com/
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/example/htdocs"
    ServerName example.org
    <Directory /var/www/example/htdocs>
         Options FollowSymLinks
         AllowOverride All
         Order Allow,Deny
         Allow from all
    </Directory>
</VirtualHost>

對於403錯誤,可能您沒有設定預設文檔,因此它嘗試存取資料夾內容。對於預設文檔,您可以使用例如

DirectoryIndex index.html Index.htm index.php

相關內容