简体   繁体   中英

ProxyPass & ProxyPassReverse - Get original URL from browser address bar

I have created two websites( http://localhost/webone , http://localhost/webtwo ).

Web two has URL like this: http://localhost/webtwo/webone (Use ProxyPass and ProxyPassReverse)

If we go to above URL, that display contents of the web one .

Now If someone access web one, then I want to catch user access URL.(It could be http://localhost/webone or http://localhost/webtwo/webone )

My Issue is:

If someone access web two from http://localhost/webtwo/webone URL. Then if I execute following code it returns http://localhost/webone .

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

But browser display URL is http://localhost/webtwo/webone . Can someone please suggest a way to catch http://localhost/webtwo/webone URL.

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

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'];

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