简体   繁体   English

$ _SERVER ['SERVER_NAME']和$ _SERVER ['HTTP_HOST']都与实际网址不同

[英]$_SERVER['SERVER_NAME'] and $_SERVER['HTTP_HOST'] both different than actual URL

When accessing the URL www.domain.com/test.php , both $_SERVER['SERVER_NAME'] and $_SERVER['HTTP_HOST'] output the domain name without the www. 当访问URL www.domain.com/test.php时$_SERVER['SERVER_NAME']$_SERVER['HTTP_HOST']输出没有www的域名 prefix: domain.com 前缀: domain.com

Why would that be this way ? 为什么会这样?

EDIT: as stated here , $_SERVER['SERVER_NAME'] could be different than the actual requested URL, however $_SERVER['HTTP_HOST'] should return the domain. 编辑:作为陈述这里$_SERVER['SERVER_NAME']可能比实际的请求的URL不同,但$_SERVER['HTTP_HOST']应该返回域。

PHP version: 7.1 Apache version : 2.2 PHP版本:7.1 Apache版本:2.2

.htaccess content is as follow (the interesting part is the top rewrite block) .htaccess内容如下(有趣的部分是顶部重写块)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>

# BEGIN Force SSL for SAKURA
# RewriteしてもHTTPS環境変数を有効にする
SetEnvIf REDIRECT_HTTPS (.*) HTTPS=$1


# 常時HTTPS化(HTTPSが無効な場合リダイレクト)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{ENV:HTTPS} !on
RewriteCond %{REQUEST_URI} !/wp-cron\.php$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END Force SSL for SAKURA

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

And here is the content of test.php: 这是test.php的内容:

<?php 
echo $_SERVER['SERVER_NAME'];

echo  $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

Output is "domain.com" and "domain.com/test". 输出是“domain.com”和“domain.com/test”。

Well that top interesting rewrite block you mentioned is removing the www. 那么你提到的顶级有趣的重写块就是删除www. from the URL with a 301 redirect. 来自具有301重定向的URL。 So there's no way of a request getting to your PHP script with the www. 因此,无法通过www.访问您的PHP脚本www. intact, because any request including the www. 完整,因为任何请求包括www. is issued a 301 redirect and never gets to the PHP script. 发出301重定向,永远不会访问PHP脚本。 The browser is then re-issuing the request based on the 301 redirect, and now it gets to your script without the www. 然后,浏览器根据301重定向重新发出请求,现在它将在没有www.情况下进入您的脚本www. .

You can test this by commenting out that top block, clearing your browser cache (important - or using a different browser), and visiting the test page again. 您可以通过注释掉顶部块,清除浏览器缓存(重要 - 或使用其他浏览器)并再次访问测试页来对此进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM