Apache mod_rewrite 更改 PHP 脚本中的真实服务器端口输出
我注意到,当使用 mod_rewrite 时,即使 apache 在端口 8080 上,PHP 也会返回 server_port 80。
即我有这个 url: http: //myvirtualhost.8080/index.php 和这个 PHP 片段:
<?php echo $_SERVER['SERVER_PORT']; ?>
在 .htaccess 中,我有一个简单的规则
RewriteEngine on
RewriteRule ^/?$ index.php [NC,L]
,打开 mod_rewrite,输出为“80”(错误)
,关闭 mod_rewrite,输出为“8080”(正确)
正常吗?有什么简单的解决方案吗? 事实是,我需要检查要在条件 PHP 脚本中使用的真实服务器端口,例如:
<?php
if ($_SERVER['SERVER_PORT'] == 80) {
do something
} else {
do something else
}
?>
当然这实际上是不可能的,因为 SERVER_PORT 上的 mod_rewrite 始终为 80。我不想弄乱 .htaccess 文件,所以 PHP 脚本解决方案会很棒...
提前致谢!
I noticed that when using mod_rewrite, PHP returns 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要设置“UseCanonicalPhysicalPort”,
这将确保重写规则将使用与请求相同的端口重写 url。
You need to set "UseCanonicalPhysicalPort"
this will ensure that rewrite rule will rewrite the url with the same port as the request.
您还可以通过添加冒号和端口号或 SERVER_PORT 变量来重写服务器端口:
www.example.com:%{SERVER_PORT}
。查看 apache 文档。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.