NGINX: How to redirect the domain with parameters in the url

nginxredirectrewrite

I have been trying to redirect a URL to another domain using URL rewriting.
Below is my nginx.conf file:

worker_processes  1;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    server {
            listen       8070;
            server_name  www.example.com;
            rewrite ^/v1/([0-9]+).html http://www.example.com/v1?exid=$1;


            location ~ /v1/([0-9]+) {
                return 301 http://dev.example1.com/v1?exid==$1;
            }
        }
}

I have been trying for a long time but I am still not able to find a proper solution.

NOTE: I didn't get any errors if I try the above but the expected redirecting does not occur.
I want this Url to be redirected
http://example.com/v1/68740.html —to—> http://dev.example1.com/v1?exid=68740.

Thanks in advance.

Best Answer

Try following configuration:


server {
  listen 8070;
  server_name www.example.com;
  rewrite_log on;

  location ~^/v1/([\d]+)\.html$ {
    return 301 http://dev.example1.com/v1?exid=$1;
  }
}

If you want to debug configuration of ngx_http_rewrite_module, set rewrite_log directive on. For details on rewrite_log :

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite_log

You had better check access_log finally your nginx returned (redirected) URL finally.