【问题标题】:I am trying to rewrite nginx address with a "?"我正在尝试用“?”重写 nginx 地址
【发布时间】:2013-12-16 23:43:11
【问题描述】:

我正在尝试完成关于 nginx 重写的两件事。首先是这样重写:

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

注意到第二个虚拟主机前面的“./”了吗?

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

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

通过在任何位置命令之前在新虚拟主机上执行此操作,我已设法使其“在某种程度上”工作:

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

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

【问题讨论】:

  • 重写还是重定向?因为你正在改变主机。
  • 301 重定向到新目录链接和文件链接。虽然我可以在原始 vhost 上对新 vhost 执行 301,然后担心新 vhost 上的重写。只要有效就没有关系。

标签: php regex mod-rewrite nginx


【解决方案1】:

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

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.4.4 用 pagespeed mod 编译。
【解决方案2】:

在 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 正在做什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2011-05-11
    • 2020-07-05
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多