我一直在精神上这个。 如何将/2013/04/test.html这样的urlredirect到/test in nginx?
我试过这个:但不起作用:
server { location /2013/05/test.html { return 301 http://$server_name/test; } }
我已经执行了一些testing – 出于某种原因,在configuration行的位置部分中没有.html扩展名的任何url都将正确redirect,但只要将.html放置在位置kaboom中,就会停止工作。
任何想法,为什么这是? 谢谢!
您可以随时将重写规则添加到现有位置块,而不是每个redirect都有一个位置块。
rewrite /2013/05/test.html http://$server_name/test permanent; rewrite /2013/05/test2.html http://$server_name/test2 permanent;
你也可以做各种正则expression式,这样你就可以避免每次添加一个新的“perma-link”时添加一个新的正则expression式。
我同意贾森Ilicic,使用重写规则可能会更有效。
如果你真的想使用位置块 ,你有没有尝试过使用“=”修饰符? 如果你有多个位置,Nginx的select逻辑可能会select一个与你期望的不同的逻辑。
来自Nginx文档
using the “=” modifier it is possible to define an exact match of URI and location
所以例如
server { location = /2013/05/test.html { return 301 http://$server_name/test; } }