2012-01-25 17 views
1

私はすべてのサブドメインをwwwに書き換える正しい正規表現を見つけようとしています。nginxはすべてのサブドメインをwwwに書き換えます

のでwww.domain.com

www.domain.com

またはalskdaslkdja.domain.comに例w7w.domain.comのために私は 私の最後の試みだった、多くのことを試してみました:

if ($host ~* (?!www\.)(.*)) 
{ 
    set $host_without_www www.$1; 
    rewrite ^(.*)$ http://$host_without_www$1 permanent; 
} 

でも、どちらも機能しませんでした。

私はこれらをキャッチする必要があります。私はいくつかのドメインがこのインスタンスで提供されているため、www.domain.com にワイルドカードリライトを行うことはできません。

答えて

1
server { 
    listen   xx.xx.xx.xx:80; 
    server_name  www.example.com; 
    # .... 
} 
server { 
    listen   xx.xx.xx.xx:80; 
    server_name  example.com *.example.com; 
    rewrite  ^http://www.example.com$request_uri? permanent; 
} 
+0

おかげで、しかし、私が言ったように、私はこのから提供されている多くのドメインを持っているので、この方法でそれを行うことができない、と私はそれらのすべてのサーバーのエントリを作成する余裕はありません。 –

0
server { 
    # Prefix server wildcards are checked before regexes, so this 
    # will catch anything starting with www. 
    server_name www.*; 
} 

server { 
    # This should redirect anything that didn't match the first 
    # server to domain.tld 

    # I think this regex will capture the domain.tld 
    server_name ~(?<domain>[^.]+\.[^.]+)$ 

    rewrite^http://www.$domain$request_uri? permanent; 
} 
+0

ああ、これはまた、私は他のサブドメインボックスでも実行しているので、これは動作しません。 (mail.example.comなど)、これは私が思っているものを壊すだろう。 –

+0

正規表現のサーバ名が最後に評価されます。サーバーがある場合{server_name mail.example.com; }またはさらにはサーバー{server_name mail。*; }それは正規表現のserver_nameの前に一致します。 – kolbyjack

関連する問題