Apache mod_rewrite changes the REAL server port output in a PHP script
I noticed that when using mod_rewrite, PHP 开发者_如何学Goreturns server_port 80 even if apache is on port 8080.
i.e I have this url: http://myvirtualhost.8080/index.php and this PHP snippet:
<?php echo $_SERVER['SERVER_PORT']; ?>
In .htaccess I have this simple rule
RewriteEngine on
RewriteRule ^/?$ index.php [NC,L]
With mod_rewrite on, output is "80" (wrong)
With mod_rewrite off, output is "8080" (correct)
Is it normal? Anysimple solutions for this? The fact is that i need to check the REAL server port to use in a conditional PHP script, something like:
<?php
if ($_SERVER['SERVER_PORT'] == 80) {
do something
} else {
do something else
}
?>
and of course this is actually not possible since with mod_rewrite on the SERVER_PORT is always 80. I prefer not to mess with .htaccess file, so a PHP script solution would be great...
Thanks in advance!
You need to set "UseCanonicalPhysicalPort"
UseCanonicalPhysicalPort on
this will ensure that rewrite rule will rewrite the url with the same port as the request.
You can rewrite the server port as well, by adding a colon and the port number, or the SERVER_PORT variable: www.example.com:%{SERVER_PORT}
. Check the apache documentation.
精彩评论