Apache 重写 - 在 PHP 中获取原始 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5493075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:37:13  来源:igfitidea点击:

Apache rewrite - get original URL in PHP

phpurlrewrite

提问by swamprunner7

I have a rewrite in nginx or Apache for this address:

我在 nginx 或 Apache 中对此地址进行了重写:

http://domain.com/hello

to a script like

像这样的脚本

http://domain.com/test.php&ref=hell

How can I access this rewritten URL in PHP? Because, if I use $_SERVER['REQUEST_URI']of course I get:

如何在 PHP 中访问这个重写的 URL?因为,如果我$_SERVER['REQUEST_URI']当然使用,我会得到:

/test.php&ref=hell

but I only want:

但我只想:

/hello

Is this possible? Thanx for help.

这可能吗?感谢您的帮助。

Upd nginx cnf

更新 nginx cnf

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

server
{
  listen 80;
  server_name domain.test;


  location /
  {
    rewrite ^/(main|best|air)$ /core/feeds.php?act= last;
    proxy_pass http://127.0.0.1:8080;
  }
}

采纳答案by swamprunner7

In Nginxconf, we need to add user header with request_uri:

Nginxconf 中,我们需要添加用户头request_uri

proxy_set_header request_uri $request_uri;

And read it in php:

并阅读它php

echo $_SERVER['HTTP_REQUEST_URI'];

upd

更新

for some reason nginx don't like symbol '_' in header name, don't know how it worked before, maybe something changed after nginx update. Now i'm using

出于某种原因,nginx 不喜欢标头名称中的符号“_”,不知道它之前是如何工作的,也许在 nginx 更新后发生了一些变化。现在我正在使用

proxy_set_header rewriteduri $request_uri;

and in php

并在 php 中

$_SERVER['HTTP_REWRITEDURI']

回答by mario

It really depends on the PHP setup. With mod_php you oftentimes still have the original request path in REQUEST_URI. For CGI or FastCGI setups it is quite commonly REDIRECT_URL. You will have to check a phpinfo()page to be sure.

这真的取决于 PHP 设置。使用 mod_php,您通常仍然在REQUEST_URI. 对于 CGI 或 FastCGI 设置,它很常见REDIRECT_URL。您必须检查一个phpinfo()页面才能确定。

If you really can't find anything that would help, then it's time for cheating! You can adapt your RewriteRule like this to retain the original URL in an environment variable of your chosing:

如果你真的找不到任何有用的东西,那么就该作弊了!您可以像这样调整您的 RewriteRule 以在您选择的环境变量中保留原始 URL:

RewriteRule ^(\w+)$   test.php?ref=    [E=ORIG_URI:/]

This would then be available as $_SERVER["ORIG_URI"], or you can just get it from the URI with $_GET['ref']. But you would have to use this trick on all potential RewriteRules.

这将作为 可用$_SERVER["ORIG_URI"],或者您可以使用 $_GET['ref'] 从 URI 中获取它。但是你必须在所有潜在的 RewriteRules 上使用这个技巧。

回答by Phil

You can usually find the requested URL in

您通常可以在

  • $_SERVER['REQUEST_URI']
  • $_SERVER['REDIRECT_URL'](maybe Apache only, don't know about nginx)
  • $_SERVER['REQUEST_URI']
  • $_SERVER['REDIRECT_URL'](也许只有 Apache,不知道 nginx)

I know you mentioned $_SERVER['REQUEST_URI']contains your rewritten URL but in all my tests, it contains the original request.

我知道你提到的$_SERVER['REQUEST_URI']包含你重写的 URL,但在我所有的测试中,它包含原始请求。

Why don't you dump $_SERVERand see what's in there.

你为什么不倾倒$_SERVER,看看里面有什么。