nginx重定向除一个以外的所有目录

Dou*_*eri 16 mod-rewrite nginx

我正在使用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;
    }
Run Code Online (Sandbox Code Playgroud)

如果我这样做,甚至www.mysite.com/blog将被重定向到谷歌搜索页面.如果我删除最后一个位置www.mysite.com/blog效果很好.

从我在这里读到的:http://wiki.nginx.org/HttpCoreModule#location似乎优先级将首先在正则表达式上,并且与查询匹配的第一个正则表达式将停止搜索.

谢谢

VBa*_*art 25

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

location /blog {
      root   html;
      index index.php;
      try_files $uri $uri/ /blog/index.php;
}
Run Code Online (Sandbox Code Playgroud)

  • 请描述该解决方案为何有效,而不是简单地粘贴代码和链接 (11认同)