当前位置: 首页 > nginx > 正文

nginx dynamic url rewrite

I have such a request, will rewrite url to google’s search site,
type http://mysite/search/search.html?t=laday gaga –> http://www.google.com/search?q=lady%20gaga
I donn’t want to use other tools(php/java etc.) except nginx.
For a long time googleing, finally find how to setting nginx dynamic url rewrite 🙂

 location /search/ {
  root   /var/2hei.net/nginx;
  if ($args){
  rewrite ^/search/search.html “http://www.google.com/search?q=$arg_t?” last;
  }
 }

and ‘?’ is very important, or the rewrite url will add append query string,
http://mysite/search/search.html?t=laday gaga –> http://www.google.com/search?q=lady%20gaga?t=lady%20gaga
we will get wrong rewrite url.
alse we can use $query_string
 if ($query_string ~* t=(.*)){
  …
 }

Just enjoy it!

本文固定链接: http://2hei.net/nginx_dynamic_url_rewrite.html | 2hei's site

该日志由 2hei 于2010年08月06日发表在 nginx 分类下,
原创文章转载请注明: nginx dynamic url rewrite | 2hei's site
关键字:

nginx dynamic url rewrite:目前有1 条留言

  1. 沙发
    fenderbirds:

    nice article, keep the posts coming

    2010-10-19 上午 1:29