I am trying to redirect all traffic from urls that contain 'www.' to the similar url that does not contain it, but when I do so Nginx appends a duplicate query string at the end of the url.
For example, the user enters:
www.website.com/test/?_id=12345
And they get redirected to:
website.com/test/?_id=12345?_id=12345
Here is my config:
server {
server_name www.website.com;
return 301 $scheme://website$request_uri permanent;
}
server {
server_name website.com
# actual server stuff
}
I have tried appending a '?' to the end of the redirect after the 'request_uri' portion because form what I have read that should work, but it didn't.
Your config is invalid. You seem to have mixed up the syntax of return and rewrite. See this document for details.
The $request_uri variable already includes the query string, so this should work with return:
return 301 $scheme://example.com$request_uri;
The rewrite directive will append the query string unless a ? is appended. It is possible to use a rewrite directive to accomplish the same function, but in this case it would be overkill.
As your config is invalid, nginx is still running with an earlier configuration, possibly an earlier experiment that placed $request_uri in a rewrite directive, hence the double query string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With