19

我正在使用 nginx 1.0.8,我正在尝试将所有访问者从 www.mysite.com/dir 重定向到谷歌搜索页面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 将被重定向到谷歌搜索页面。如果我删除最后一个位置 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

This situation can also be handled using only regex. Though this is a very old question and it has been marked answered, I'm adding another solution.

If you use multiple loop forwards using reverse proxy this is the easiest way without having to add a separate location block for every directory.

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 回答