XuYuanzhen
XuYuanzhen

Reputation: 3

Nginx multiple conditions for rewrite

my nginx configuration as following

  location /eicnews/ {
  set $con "";

  if ($request_uri ~ ^/eicnews\/(.*)$) {
    set $con  A;
  }

  if ($request_uri ~ ^/eicnews\/$) {
    set $con  "${con}B";
  }

  if ($con = A) {
rewrite ^/eicnews\/eicnewscontent_(.*)\.shtml$ http://news.abc.cn/eic/news_$1.shtml permanent;
        }

        if ($con = AB) {
rewrite ^/eicnews\/(.*)$ http://news.abc.cn/eic permanent;

        }

}

my configuration means that 1.if clients request directoty named "/eicnews/" will be rewrited to url/dir 2.if clients request the files included the directoty named "/eicnews/" will be rewrited to url/dir/files

But now there tow kinds file names in "/eicnews/" ,eicnewscontent_xxxx.shtml and eicnewslist_xxxx.shtml ,I wanna if the prefix "eicnewscontent" files be rewrited to http://news.abc.cn/eic/news_$1.shtml and the prefix "eicnewslist" will be rewrited to http://news.abc.cn/eic.

Thank You ALL!!!!

I mean that,there 2 kinds prefix file in the directoty,such as eicnewslist_xxxxx.shtml and eicnewscontent_xxxxx.shtml, prefix "eicnewslist" rewrite to one url,prefix "eicnewscontent" rewrite to the other url.I want it as following.Syntax is OK. But it doesn't works.

location =  /eicnews/ {

        rewrite  ^/eicnews/eicnewslist(_.*\.shtml)$  http://news.smartjs.cn/eic$1 permanent;
    }
    location /eicnews/ {
        rewrite ^/eicnews/eicnewscontent(_.*\.shtml)$ http://news.smartjs.cn/eic/news_xx.shtml$1 permanent;
    }

Upvotes: 0

Views: 1798

Answers (1)

Alexey Ten
Alexey Ten

Reputation: 14354

Try to avoid ifs as much as possible. Read http://wiki.nginx.org/IfIsEvil.

In your case I would do following:

location = /eicnews/ {
    rewrite ^ http://news.abc.cn/eic permanent;
}

location /eicnews/ {
    rewrite ^/eicnews/eicnewscontent(_.*\.shtml)$ http://news.abc.cn/eic/news$1 permanent;
}

I'm not sure what you suppose to do with requests to /eicnews/other-path.

Upvotes: 1

Related Questions