I've deployed a site live and trying to redirect some of the original URLs to the new URLs. I'm fairly new with NGINX configuration on redirects like this.
This one works:
location /contactus.aspx {
return 301 $scheme://$host/contacts;
}
These do not work (The url results in a 404 and does not redirect):
location /productcatalog.aspx?directoryid=11 {
return 301 $scheme://$host/hard-drive-cases;
}
location /productdetails.aspx?productid=26* {
return 301 $scheme://$host/lto-5-blue;
}
I've already service nginx reload
successfully without error.
The biggest difference between the redirects that do work and the redirects that do not work are the added parameters. What is the best way to redirect URLs with parameters (and a wild card at the end) so that it will work properly?
Best Answer
The
location
directive does not match on the query string. So you'll have to do something else.I presume you have a large number of these to work with, so I would suggest using a couple of
map
s. For instance:Then you would do a
location
such as:Make a second
map
andlocation
for the$arg_productid
corresponding toproductdetails.aspx
. If that one is exceptionally large, though, you might have performance issues, and need to scrap this and do some scripting to obtain the redirect from a database.The
map
s must be in yourhttp
block, not within aserver
block. If you are hosting multiple sites, the best place to put them, in my opinion, is immediately before theserver
block that they correspond to.