When accessing the URL www.domain.com/test.php, both $_SERVER['SERVER_NAME']
and $_SERVER['HTTP_HOST']
output the domain name without the www. prefix: 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.
PHP version: 7.1 Apache version : 2.2
.htaccess content is as follow (the interesting part is the top rewrite block)
<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:
<?php
echo $_SERVER['SERVER_NAME'];
echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
Output is "domain.com" and "domain.com/test".