Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx redirect all directories except one

I'm using nginx 1.0.8 and I'm trying to redirect all visitors from www.mysite.com/dir to google search page http://www.google.com/search?q=dir where dir is a variable, however if dir=="blog"( www.mysite.com/blog) I just want to load the blog content(Wordpress).

Here is my config :

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

if I do this even www.mysite.com/blog will be redirected to google search page. If I delete the last location www.mysite.com/blog works great.

From what I've read here: http://wiki.nginx.org/HttpCoreModule#location it seems that the priority will be first on regular expressions and that first regular expression that matches the query will stop the search.

Thanks

like image 360
Doua Beri Avatar asked Jul 15 '12 15:07

Doua Beri


1 Answers

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

location /blog {
      root   html;
      index index.php;
      try_files $uri $uri/ /blog/index.php;
}
  • http://nginx.org/r/location
  • http://nginx.org/en/docs/http/request_processing.html
like image 52
VBart Avatar answered Oct 15 '22 23:10

VBart