Akhilesh
Akhilesh

Reputation: 1104

URL redirect with params in Nginx

In , I am redirecting a URL with a param like:

www.example.com/watch?v=12345678 -> www.example.com?vid=12345678

So I wrote the following configuration for the same:

location  /watch {
    if ($args ~* v=(.*)) {
        set $args vid=$1;
        rewrite ^/watch?(.*)$ / redirect; 
    }
}

Everything is working as expected. But here I have few queries:

Upvotes: 2

Views: 6433

Answers (1)

Bernard Rosset
Bernard Rosset

Reputation: 4763

I have nothing better than:

location /watch {
    return 302 $scheme://$host?vid=$arg_v;
}

It avoids using if and rewrite, which is always a good thing. If you need to check for v= in the URI, then use if, but rather test whether $arg_v is empty.

However it redirects even if there is no argument or if it is empty. I guess you should filter those case out of the redirected location.

From a SEO prospective, I do not see any difference. I would avoid serving any URI with arguments, though. I would prefer URIs like example.com/video/123456

Upvotes: 6

Related Questions