3

I am using nginx 1.14.0 on Ubuntu.

I want anyone coming to https://www.example.com/blog/authors/ should always redirected to https://www.example.com/blog which is working fine, but if anyone comes through https://www.example.com/blog/authors/?hello (with question mark) is being forwarded to https://www.example.com/blog/?hello

Why is it doing like this? and how can I correct my nginx configuration settings so even if anyone types in anything with question mark '?' to https://www.example.com/blog/authors/?hello should always forwarded to https://www.example.com/blog/ as well.

This is my redirect rule

# Redirects to handle all of the URL patterns from the old site
    rewrite ^/blog/authors/$ /blog/$1 permanent; 

And, following is my complete nginx config block file

    server {
        listen 80;
        listen 443 ssl;
        server_name example.com www.example.com;
        access_log /var/log/nginx/example.com.access.log;

        ssl_certificate      /etc/nginx/ssl/example.com.crt;
        ssl_certificate_key  /etc/nginx/ssl/example.com.key;
        ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;

        location = /favicon.ico { access_log off; log_not_found off; }

        location /static/ {
                root /my/server/site/path;
        }

        location / {
                include proxy_params;
                proxy_pass http://unix:/my/server/site/path/site.sock;

        }

        # Redirects to handle all of the URL patterns from the old site
            rewrite ^/blog/authors/$ /blog/$1 permanent; 
}

I have seen this answer to other question https://stackoverflow.com/questions/44782411/nginx-rewrite-question-mark but not sure exactly where do I need to make appropriate changes?

Pothi Kalimuthu
  • 5,734
  • 2
  • 24
  • 37
Farmi
  • 369
  • 1
  • 4
  • 17

1 Answers1

3

Why is it doing like this?

The official answer is at https://nginx.org/r/rewrite . Here's the direct quote from the above URL...

If a replacement string includes the new request arguments, the previous request arguments are appended after them. If this is undesired, putting a question mark at the end of a replacement string avoids having them appended

So the correct statement would be...

rewrite ^/blog/authors/$ /blog/? permanent;

or

rewrite ^/blog/authors/$ /blog? permanent;

... depending on if you'd like the trailing slash or not.

Only if we have a regular expression, rewrite is needed. All other redirects can be done using return statement that is faster due to lack of evaluation of regular expression.

For your use-case, my existing answer for a similar question would work. Here's the complete answer (for redirecting without query strings), though...

location /blog/authors {
    return 301 /blog;
}

Here's the complete server block...

server {
    listen 80;
    listen 443 ssl;

    server_name example.com www.example.com;

    root /my/server/site/path; # this line is needed to the location blocked added for redirection.

    access_log /var/log/nginx/example.com.access.log;

    ssl_certificate      /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key  /etc/nginx/ssl/example.com.key;
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
            root /my/server/site/path;
    }

    location / {
            include proxy_params;
            proxy_pass http://unix:/my/server/site/path/site.sock;
    }

    # Redirects to handle all of the URL patterns from the old site
    location /blog/authors {
        return 301 /blog;
    }
}

I hope that clarifies and helps.

Pothi Kalimuthu
  • 5,734
  • 2
  • 24
  • 37
  • 2
    Thanks, ending it with `?` worked perfectly like `rewrite ^/blog/authors/$ /blog/? permanent;` it worked for me. Finally, can you please tell me in layman sense that what is the difference will I get if I use return instead of rewrite as currently rewrite is working fine. – Farmi Jul 03 '19 at 11:15
  • There is no functional difference. The only difference is in speed at which the statement is executed. You may ignore that speed difference too, if you don't have large traffic or traffic spikes. – Pothi Kalimuthu Jul 03 '19 at 11:17