我不得不在nginx中redirect一些旧的url。 旧的url是旧式的PHPurl,包括加载内容的参数。 他们看起来像这样:
http://www.foo.com/index.php?site=foo http://www.foo.com/index.php?site=bar
我想redirect他们到其他url,如:
http://www.foo.com/news http://www.foo.com/gallery
任何意见,我怎么能做到这一点? 我的尝试失败了。 提前致谢!
如果在旧的URL参数和新的URLpath之间有一个直接映射,那么可以使用这样的语法:
location /index.php { ... try_files $uri /$arg_site =404; }
有了这样的configuration,任何以/index.php开头的请求都会首先被重新尝试,如果没有匹配的文件或位置,如果原始URL是/index.php?site=foo会尝试使用/foo进行内部redirect/index.php?site=foo 。 如果这也失败了,nginx会发送一个404 HTTP Not Found响应。
variables$arg_site被设置为URL参数site的值。
我通过使用PHP而不是nginx重写function解决了这个问题:
if (isset($_GET["site"]) { switch ($_GET["site"]) { case 'foo': Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: /foo" ); break; ... } }