Nginx rewrite issue with question mark
Solution 1:
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.