19

nginx 1.0.8 を使用しており、すべての訪問者を www.mysite.com/dir から Google 検索ページhttp://www.google.com/search?q=dirにリダイレクトしようとしています。 ここで、dir は変数です。ただし、dir=="blog"( www.mysite.com/blog) の場合、ブログのコンテンツ (Wordpress) をロードしたいだけです。

ここに私の設定があります:

    location / {
        root   html;
        index  index.html index.htm index.php;
    }



    location /blog {
          root   html;
          index index.php;
          try_files $uri $uri/ /blog/index.php;
    }

    location ~ ^/(.*)$ {
          root   html;
          rewrite ^/(.*) http://www.google.com/search?q=$1 permanent;
    }

これを行うと、www.mysite.com/blog でも Google 検索ページにリダイレクトされます。最後の場所 www.mysite.com/blog を削除するとうまくいきます。

私がここで読んだことから: http://wiki.nginx.org/HttpCoreModule#location正規表現が優先され、クエリに一致する最初の正規表現が検索を停止するようです。

ありがとう

4

2 に答える 2

28
location / {
    rewrite ^/(.*)$ http://www.google.com/search?q=$1 permanent;
}

location /blog {
      root   html;
      index index.php;
      try_files $uri $uri/ /blog/index.php;
}
于 2012-07-17T14:39:33.217 に答える
0

この状況は、正規表現のみを使用して処理することもできます。これは非常に古い質問であり、回答済みとマークされていますが、別の解決策を追加しています。

リバース プロキシを使用して複数のループ フォワードを使用する場合、これが最も簡単な方法であり、ディレクトリごとに個別のロケーション ブロックを追加する必要はありません。

root html;
index index.php;

location / { #Match all dir
      try_files $uri $uri/ $uri/index.php;
}

location ~ /(?!blog|item2)(.*)$ { #Match all dir except those dir items skipped
      rewrite ^/(.*) http://www.google.com/search?q=$1 permanent;
}
于 2020-01-04T10:27:26.880 に答える