2017-01-04 2 views
0

すべてのトラフィックをルートドメインにwwwにリダイレクトします。バージョンを維持しながらパス/クエリ文字列をそのまま残して、どうすればいいのですか?私が何をしないのです - example.com/abc?page=1 =>www.example.com/abc?page=1:私は実際にそれを行うにwan't example.com/abc?page=1 =>www.example.com、:すべてのルートドメイン要求をwwwにリダイレクトします。 URLをそのまま維持するサブドメイン

frontend http 
     bind *:80 
     bind *:443 

     acl has_www hdr_beg(host) -i www 
     http-request redirect code 301 location http://www.%[hdr(host)]%[req.uri] unless has_www 

しかしこれは、以下を行います。私はHAproxy設定等は次のセットアップを持っていますか?

+0

あなたはどのバージョンのHAProxyを使用していますか? –

答えて

1

あなたはHAProxy 1.5を使用していて、アップこれが機能するかどうかについて、あなた

単一ドメイン:(汚いけど作品)

# match non www requests: 
acl has_www  hdr_beg(host) -i www. 

# add a header that says we need to redirect these type of requests (will explain later) 
http-request add-header X-Host-Redirect yes unless has_www 

# rule to identify newly added headers 
acl www_redirect hdr_cnt(X-Host-Redirect) eq 1 

# where the magic happens (insert www. in front of all marked requests) 
reqirep ^Host:\ (.*)$ Host:\ www.\1 if www_redirect 

# now hostname contains 'www.' so we can redirect to the same url 
    redirect scheme http if www_redirect 

acl has_www hdr_beg(host) -i www 
redirect prefix http://www.example.com code 301 unless has_www 

複数のドメイン新しいヘッダーを追加する理由は、HAProxy 1.5以降では、ACLがbei各基準で評価された。新しいヘッダーなしでこれを実行しようとすると、次のようになります。

#catch all domains that begin with 'www.' 
acl has_www  hdr_beg(host) -i www. 
#insert www. in front of all non www requests 
reqirep ^Host:\ (.*)$ Host:\ www.\1 unless has_www 
#now hostname contains 'www.' so when the next rule gets interpreted it does not match then fails 
#redirect to new url 
redirect code 301 prefix/if has_www 

これが役立ちます。私はHAProxy 1.5に5個の異なるドメインでこれをテストしてみた、それがうまく働いたとパスとクエリパラメータ保存リダイレクトするHAProxy 1.6のリダイレクト

+0

しかし、example.comだけでなく、どんなドメイン名でも動作する必要があります。 a_domain.se/abc?page=1 => www.a_domain.se/abc?page=1 –

+0

複数のドメインを含めるように答えを更新しました –

+0

@LouisKriekルートドメイン、例えば、 g。 'example.com - > www.example.com'ではなく' sub1.example.com'ですか? – TylerDurden

0

マルチドメインソリューションの完全なクエリ文字列を保持:

frontend 443 
    http-request redirect prefix https://www.%[hdr(host)] code 301 unless { hdr_beg(host) -i www. } 
関連する問題