【问题标题】:nginx redirect all directories except onenginx重定向除一个以外的所有目录
【发布时间】:2012-07-14 15:39:25
【问题描述】:

我正在使用 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 似乎优先级将首先放在正则表达式上,并且与查询匹配的第一个正则表达式将停止搜索。

谢谢

【问题讨论】:

    标签: mod-rewrite nginx


    【解决方案1】:

    这种情况也可以只使用正则表达式来处理。虽然这是一个非常古老的问题并且已被标记为已回答,但我正在添加另一个解决方案。

    如果您使用反向代理使用多个循环转发,这是最简单的方法,无需为每个目录添加单独的位置块。

    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;
    }
    

    【讨论】:

      【解决方案2】:
      location / {
          rewrite ^/(.*)$ http://www.google.com/search?q=$1 permanent;
      }
      
      location /blog {
            root   html;
            index index.php;
            try_files $uri $uri/ /blog/index.php;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-23
        • 1970-01-01
        • 2016-11-12
        • 2019-07-13
        • 2013-09-23
        • 2015-09-29
        • 2013-04-28
        • 2010-10-29
        相关资源
        最近更新 更多