Nginx重写网址以获得干净的网址

时间:2014-07-27 19:58:57

标签: nginx rewrite

我有一个网站,网址低于url,我在nginx上运行

http://example.com/category.php?page=1&query=latest

现在我的目标是使用nginx rewrite将其转换为下面的

http://example.com/latest/1/

其中1是页码,是动态的

获得干净网址的确切规则是什么,

除此之外,我还需要一个规则,如果有人为页面提供负值,它将自动重定向到页码的正值。

现在我现有的配置如下


 server {
        server_name example.com;
        error_log    /data1/nginx/aggri.com.error.log;

        root /data1/aggri;
        index index.php;
        rewrite_log on;
        location ~ .php$ {

                 try_files $uri $uri/ /index.php;
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                expires       0;
                 add_header    Cache-Control  public;
                set $skip_cache 0;
if ($request_uri ~* "^(.*/)index\.php$") {
        return 301 $1;
    }        }

        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|js|woff|css)$ {
                access_log off; log_not_found off; expires max;
        }

        location = /robots.txt { access_log off; log_not_found off; }
        location ~ /\. { deny  all; access_log off; log_not_found off; }
}

1 个答案:

答案 0 :(得分:0)

示例:

location  ~ /category.php$ {
   if ( $args ~ ^page=[-]*([0-9]+)&query=([\w]+)$ ) {
      set $pg $1;
      set $q  $2; 
      rewrite (.*)  /$q/$pg/? permanent;
   }
}