【问题标题】:nginx rewrite php file to another php filenginx 将 php 文件重写为另一个 php 文件
【发布时间】:2014-10-11 21:11:49
【问题描述】:

我已经在网上找到了大量资源,但我尝试过的方法似乎都不起作用。

我有一个旧网址,有时会被调用,如下所示:

http://domain.com/forum/index.php?app=subscriptions&r_f_g=xxx-paypal

xxx 每次都会变化。

每当这个页面被点击时,我希望它使用 domain.com/index.php。

注意我希望它重定向,我希望请求 URI 保持不变,我只希望根目录下的 php 脚本运行,被调用的 php 脚本不存在.

到目前为止,我的实验只用了

http://domain.com/forum/index.php

但理想情况下,我也想让它与 get 变量一起使用。下面的示例不包含它们的唯一原因是因为我什至无法让这个基本的 index.php 工作,并且摆弄变量会使事情变得过于复杂。

这是我迄今为止基于谷歌搜索所尝试的。他们都没有做任何事情,当我访问“forum/index.php”时,他们都只是返回“404 not found”

location /forum/index.php {
    rewrite ^ /index.php last;
}

alias ^/forum/index.php$ /;

location /forum/index.php {
    alias /index.php;
}

location ^(.*)$ {
    try_files $uri $uri/ /index.php?$1;
}

我不知道还能尝试什么,也不知道为什么这些都不起作用。理想情况下,我希望将不存在的任何内容重写为 index.php,但现在这并不重要,我只想获得这个 forum/index.php,但到目前为止我还没有运气。

有人能指出正确的方向吗?

回顾一下:

问:当有人点击 /forum/index.php 我希望它使用 /index.php 但我不想要重定向。

【问题讨论】:

  • 请接受答案或添加您自己的答案并接受。这样做可以防止出现这个问题没有答案。 @robjbrain

标签: php .htaccess redirect nginx rewrite


【解决方案1】:

您的意思是您想要内部重定向而不是外部重定向。

我也遇到过这种问题。这就是我修复它的方法:

location / {
  error_page 556 = @rewrite;
  try_files $uri $uri/ =556;
}

location @rewrite {
  rewrite ^/(.*)$ /index.php?$1&$args last;
}

这只是又快又脏。也许更了解 nginx 的人知道比这更好的解决方案。但它仍然对我有用。

【讨论】:

    【解决方案2】:

    你试过吗?

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-12
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 2015-09-12
      • 2012-10-29
      • 2013-07-19
      相关资源
      最近更新 更多