user3136348
user3136348

Reputation: 55

Nginx rewrite url to attain clean url

I have a site with below url structure which i am running on nginx

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

Now my aim is to convert it to the below one using nginx rewrite

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

where 1 is the page number and is dynamic

What will be the exact rule to attain clean url ,

Along with it i also require a rule incase someone gives a negative value for page it will automatically get redirected to positive value of page number.

Right now my existing configuration is as below


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

Upvotes: 0

Views: 252

Answers (1)

Rhim
Rhim

Reputation: 674

Example:

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

Upvotes: 0

Related Questions