すべてのサブドメインを仮想ホスト内のメインドメインにリダイレクトする

すべてのサブドメインを仮想ホスト内のメインドメインにリダイレクトする

私は、仮想ホストにまだ記載されていないすべてのサブドメインを、空のサブドメインにリダイレクトしたいだけです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 Forbiddenが表示されますabc.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

関連情報