我试图用“?”重写nginx地址

时间:2013-12-01 03:34:32

标签: php regex mod-rewrite nginx

我正在努力完成关于nginx重写的两件事。首先是重写这样的东西:

 oldvhost.domain.com/?dir=Dir1/Dir2/Dir3 -->
 newvhost.domain.com/?dir=./Dir1/Dir2/Dir3

注意第二个vhost前面的“./”?

其次我试图改写这样的东西:

oldvhost.domain.com/orginal.php?file=Dir1/Dir2/Dir3/file.zip -> 
newvhost.domain.com/newphpfile.php?file=./Dir1/Dir2/Dir3/file.zip

我已经设法通过在任何位置命令之前在新的vhost上执行此操作来“稍微”工作:

 rewrite ^/original.php$ /newphpfile.php$1 last;

但这不是100%工作,只能通过$realpath PHP函数来解决。我仍然需要通过正则表达式重写来完成这项工作,但是有一些关于“?”的东西正在使它失败。

2 个答案:

答案 0 :(得分:0)

至于重定向,您可以在某个位置执行此操作

location /something
  return 301 http://example.com/?dir=./$arg_dir;
}

或者如果你想要它作为重写

rewrite /old-example.com/location-from.php http://example.com/new-location.php?./$arg_dir permanent;

并且对于重写,它应该与第二个重定向类似,但不需要完整的主机名

rewrite /old-location.php /new-location.php?./$arg_dir;

这是documentation of the $arg_name

答案 1 :(得分:0)

在Nginx中执行此操作的最佳方法是使用reg表达式重写。在虚拟主机中尝试以下代码。

location / {
    #Rewrite for directory
    rewrite ^/?dir=(.*) http://yoururl.com/?dir=./$1;
    #rewrite for file
    rewrite ^/origional.php?file=(.*) http://yoururl.com/newphpfile.php?file=./$1;
}

第一次重写会处理您的目录。请注意,假设所有传入链接都没有您需要的./。如果它们带有./,则可能导致破损。或者它可能什么都不会,取决于PHP正在做什么。