1

Say you've got an Apache2 virtual host setup, something like this:

/htdocs/parent1.com
/htdocs/sub1.parent1.com
/htdocs/sub2.parent1.com
/htdocs/parent2.net
/htdocs/parentn.org

Say you'd like to do this with VirtualDocumentRoot /htdocs/%0, so that you can add and remove virtual hosts without tinkering with your Apache configuration. That's important: please please no messing with htaccess files or httpd.conf every time a virtual host comes or goes - whether that host is a parent domain or not. In fact, say you're using AllowOverride None.

Anyway, the question is, how might you 301 redirect non-existent sub-domains to their corresponding parent domains without redirecting existent sub-domains?

4

1 に答える 1

1

私は自分の問題を解決したかもしれません。ただし、誰かが私がしていることに問題を見つけた場合は、フィードバックをいただければ幸いです。

以下は、対応するドキュメントルートが存在する限り、任意のサブドメインへのリクエストをそのままにします。ただし、ファイルシステムに存在しないサブドメインにリクエストをリダイレクトします。

<IfModule rewrite_module>
    RewriteEngine On
    RewriteMap lowercase int:tolower
    RewriteCond "/htdocs/${lowercase:%{HTTP_HOST}}" !-d
    RewriteCond %{HTTP_HOST} "\.([^\.]+\.[^\.]+)$"
    RewriteRule ^/(.*)$ "http://%1/$1" [R=301]
</IfModule>

構成設定に触れることなく、ワイルドカード DNS をセットアップし、名前ベースの仮想ホスティングを使用できます。また、関連する htaccess はありません。「/htdocs/[host.]domain.tld」のような任意の名前でフォルダを作成するだけで、すぐに実行できます。私が知る限り、これは実際には SSL/TLS では機能しませんが (おそらく何か関係があるの%{HTTP_HOST}でしょうか?)、安全なサイトは比較的少なく、ホスト名よりも IP アドレスで解決する方が適切です。

于 2012-07-04T13:44:37.370 に答える