Apache 和 PHP 显示错误的端口号
我目前正在尝试重新配置 Apache 服务器,以便在访问网页时能够使用 80 之外的其他端口,然后根据请求完成的端口,我执行一些 .htaccess 魔法。重新配置 Vhost 并侦听任何其他端口没有问题,但由于某种原因,所有非 ssl 请求始终被解释为到达端口 80。
如果我尝试打开页面 my.site.com:8080 我的日志显示像这样的东西
my.site.com:80 my.ip。 - - [2012年1月23日:14:37:24 +0100]“获取 /images/pagenav_bg.gif HTTP/1.1" 200 2484 “http://my.site.com:8080/css/all.css”“Mozilla/5.0(Windows NT 6.1; 哇64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"
我的 .htaccessrules 具有条件“RewriteCond %{SERVER_PORT} ^80$”,并且 PHP $_SERVER['SERVER_PORT']; 显示端口 80
。尝试过不同的端口,我什至禁用了端口 80 上的监听,这给出了 my.site.com 的预期结果停止工作,但 my.site.com:8080 仍然有效。
所以要清楚询问:如何启用/重新配置 apache 以便使用“真实”端口号?
编辑:我刚刚发现这可能有 ,我也发现 .htaccess 将端口解释为 80。
但我还没有找到如何规避它,即使在任何重写规则运行之前 我推断问题可能出在我的 .htaccess 文件中。 目前看起来是这样的,
RewriteEngine On
#Rewrite rule to allow normal access to existing files
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
#The normal rewriterule for the framework that is used
RewriteRule ^.*$ index.php [NC,L]
我相信正在发生以下情况。重写完成后,它来自 http://my.site.com:8080/some/url 被 Apache 解释为 my.site.com/index.php,默认端口为 80。我必须将其重写为 my.site.com:8080/index.php。然而,以下片段不起作用。
RewriteCond %{SERVER_PORT} ^8080$
RewriteRule ^(.*)$ http://my.site.com:%{SERVER_PORT}/index.php [NC,L]
I'm currently trying to reconfigure a Apache server to be able to use another port than 80 when accessing a webpage, and then depending on what port a request was done I do some .htaccess magic. There is no problems with reconfiguring the Vhost and listen to any other port, but for some reason all non-ssl requests are always interpreted as coming to port 80.
If I try to open the page my.site.com:8080 my log shows something like this
my.site.com:80 my.ip. - - [23/Jan/2012:14:37:24 +0100] "GET
/images/pagenav_bg.gif HTTP/1.1" 200 2484
"http://my.site.com:8080/css/all.css" "Mozilla/5.0 (Windows NT 6.1;
WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"
My .htaccessrules that have the condition "RewriteCond %{SERVER_PORT} ^80$" kicks in, and PHP $_SERVER['SERVER_PORT']; shows port 80.
I've tried with different ports and I've even disabled listening on port 80, which gives the expected result that my.site.com stops working, but my.site.com:8080 still works.
So to be clear about the questing: How do I enable/reconfigure apache so that the "real" port number is used?
Edit: I just found out that this might have to do with mod_rewrite. But I haven't found out how to circumvent it. I also find it a bit strange that .htaccess interpretates the port as 80 even before any rewrite rules run.
Edit2: After some more experimenting I've deducted that the problem is probably in my .htaccess file.
At the moment it looks like this
RewriteEngine On
#Rewrite rule to allow normal access to existing files
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
#The normal rewriterule for the framework that is used
RewriteRule ^.*$ index.php [NC,L]
I believe the following is happening. When the rewrite is done it goes from http://my.site.com:8080/some/url to be interpreted as my.site.com/index.php by Apache, which by default is port 80. I must rewrite it to my.site.com:8080/index.php instead. The following snipped does not work however.
RewriteCond %{SERVER_PORT} ^8080$
RewriteRule ^(.*)$ http://my.site.com:%{SERVER_PORT}/index.php [NC,L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以更新您的 apache 配置以包括:
窃取自: https://stackoverflow.com/a/10892718/796999
You can update your apache configuration to include:
Stolen from: https://stackoverflow.com/a/10892718/796999
我找到了答案,并且重定向现在可以正常工作。不幸的是,这意味着我必须对重定向进行硬编码,但在这种情况下这是可以接受的。
问题是,正如上面对我的问题的编辑中所述,Apache 和 .htaccess 其中 my.site.com:8080/some/url 被重写为 my.site.com/index.php,而不是 my.site。 com:8080/index.php。
这是通过为需要特定情况的每个端口添加一条规则来解决的,例如如
我在原始问题中的编辑所示,这一开始不起作用,但这只是因为我的规则顺序错误。问题解决了。
I've found the answer and the redirects now work correctly. Unfortunately this means that I have to hard-code my redirects, but in this case it is acceptable.
The problem was, as described in the edit to my question above, Apache and the .htaccess where my.site.com:8080/some/url was rewritten to my.site.com/index.php, and not my.site.com:8080/index.php.
this was solved by adding one rule for each port where a specific case was needed such as
As seen in my edits in the original questions, this did not work at first, but that was only because my rules came in the wrong order. Problem solved.