NGINX 301将带有参数的URLredirect到新的URL

我部署了一个网站,试图将一些原始urlredirect到新的url。 对于像这样的redirect,我对于NGINXconfiguration相当新颖。

这个工作:

location /contactus.aspx { return 301 $scheme://$host/contacts; } 

这些不起作用(url结果为404,不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; } 

我已经service nginx reload没有错误。

工作的redirect和不工作的redirect之间的最大区别是添加的参数。 redirect带参数的URL(最后是一个通配符)的最好方法是什么,以便它能正常工作?

location指令在查询string上不匹配。 所以你必须做别的。

我认为你有大量的这些工作,所以我build议使用几个map 。 例如:

 map $arg_directoryid $mycategory { 11 hard-drive-cases; 12 some-other-category; default ""; # would go to the homepage, change it to go to some other page } 

然后你会做一个location ,如:

 location /productcatalog.aspx { return 301 $scheme://$host/$mycategory; } 

制作与$arg_productid对应的$arg_productid的第二张maplocation 。 但是,如果这个特别大,你可能会遇到性能问题,并且需要废除这个并且做一些脚本来从数据库获取redirect。

map必须在您的http块中,而不在server块中。 如果你要托pipe多个站点,在我看来,放置它们的最好的地方就在它们对应的server块之前。