ProxyPass和ProxyPassReverse - 从浏览器地址栏获取原始URL

我创建了两个网站( http://localhost/webone, http://localhost/webtwo).

第二个网站具有如下网址: http://localhost/webtwo/webone (使用ProxyPass和ProxyPassReverse)

如果我们转到上面的URL,上面就将会显示网络内容。 现在,如果有人访问一个Web,那么我想捕获用户访问URL。(可以是http:// localhost / webone或http:// localhost / webtwo / webone) 我的问题是: 如果有人从http:// localhost / webtwo / webone URL访问第二个网站。 然后,如果我执行以下代码,它将返回http:// localhost / webone。.

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

但是浏览器显示的URL是http://localhost/webtwo/webone. 有人可以建议一种捕获URL的方法吗?http://localhost/webtwo/webone

You can add your custom request header by using RequestHeader directive to request headers list (right after ProxyPass*):

RequestHeader add X-REQUEST-URI "expr=%{HTTP_HOST}%{REQUEST_URI}"

This way you will have a header named HTTP_X_REQUEST_URI, a holder of requested URI and is accessible via $_SERVER['HTTP_X_REQUEST_URI']:

'HTTP_X_REQUEST_URI' => string 'localhost/webtwo/webone' (length=23)

Also there are some headers which are set by mod_proxy and you may find useful. From apache.org:

When acting in a reverse-proxy mode (using the ProxyPass directive, for example), mod_proxy_http adds several request headers in order to pass information to the origin server. These headers are:

X-Forwarded-For The IP address of the client.

X-Forwarded-Host The original host requested by the client in the Host HTTP request header.

X-Forwarded-Server The hostname of the proxy server.

ProxyPass adds X-Forwarded-Host header, which contains original host, and is accessible in php as $_SERVER['HTTP_X_FORWARDED_HOST'].

Your code can be something like this:

$host = $_SERVER['HTTP_HOST'];
if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
    $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
}
$actual_link = "http://" . $host . $_SERVER['REQUEST_URI'];

When you are behind a Proxy:

Use $_SERVER['HTTP_X_FORWARDED_FOR'] in place of $_SERVER['REMOTE_ADDR']

Use $_SERVER['HTTP_X_FORWARDED_HOST'] and $_SERVER['HTTP_X_FORWARDED_SERVER']
in place of $_SERVER['SERVER_NAME']

http://php.net/manual/de/reserved.variables.server.php