Nginx重写PHP文件到另一个PHP文件


nginx rewrite php file to another php file

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

我有一个旧的url有时会被调用它看起来像这样:

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,但现在这并不重要,我只想得到这个论坛/index。php但到目前为止,我还没有任何运气

谁能给我指个正确的方向?

回顾一下:

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

你的意思是你想要一个内部的重定向,而不是一个外部的重定向。

我也有这样的问题。我是这样修复的:

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

这只是一个快速&脏了。也许更了解nginx的人知道比这更好的解决方案。

你试过吗?

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