简体   繁体   中英

nginx rewrite php file to another php file

I've already found a ton of resources on this online but nothing of what i've tried seems to work.

I have an old url that sometimes gets called that looks like this:

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

xxx changes every time.

Whenever this page is hit I want it to use domain.com/index.php instead.

Note I don't want it to redirect, I want the request URI to stay the same, I just want the php script at the root to run, the php script being called does not exist.

My experiments have so far been with just

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

but ideally I want to get this working with the get variables as well. The only reason the examples below don't include them is because I can't even get this basic index.php working and fiddling with the variables just overcomplicates things.

This is what i've tried so far based on googling. None of them do anything, they all just return "404 not found" when I access "forum/index.php"

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;
}

I don't know what else to try and I don't know why none these work. Ideally I want anything that doesn't exist to be rewritten to index.php, but right now that's kind of not important I just want to get this one forum/index.php but so far I haven't had any luck.

Can someone point me in the right direction?

To recap:

Q: When someone hits /forum/index.php I want it to use /index.php but I do not want a redirect.

You mean you want an internal redirect instead of an external redirect.

I had that kind of issue too. This is how I fixed it:

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

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

This is just quick & dirty. Maybe someone who knows nginx better knows a better solution than this. But it works for me nontheless.

have you tried this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM