0

Apache user here so not very familiar with nginx :)

Eg url like this

testpage.no/products?test_category=434

How to redirect this url and its content to this url:

testpage.no/testcategory

is this correct way if not what am I missing:

location / {
if ($arg_test_category = 434 ) {
    return 301 testpage.no/testcategory;
}
try_files $uri $uri/ /index.php$is_args$args;
}
Trenox
  • 101

2 Answers2

1

More like:

location /products {
    if ($arg_test_category = 434 ) {
        return 301 testpage.no/testcategory;
    }
}
Danila Vershinin
  • 5,286
  • 5
  • 17
  • 21
0

I recommend you to use "map". "map" should be in http section of nginx.conf. Something like this:

    http {
           ........
           map $arg_test_category $rwurl {
           default "";
           434     "testcategory";
           435     "testcategory1";
           436     "testcategory2";
           }
           ........
           server {
                  listen 80;
                  root /var/www/example.com;
                  if ($rwurl) { return 301 http://example.com/$rwurl; }

                  location / {
                      try_files $uri $uri/ /index.php$is_args$args;
                  }

                  location ~ \.php$ { 
                      fastcgi_pass 127.0.0.1:9000;
                      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                      include fastcgi_params;
                  }
    }

As you can see in http section i defined "map". If agrument is 434 then var $rwurl = "testcategory". In server section if $rwurl is something then 301 http://example.com/$rwurl. Then just usual config for nginx and php_fpm. Please read more about map here: https://nginx.org/en/docs/http/ngx_http_map_module.html