1

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".

TheTrueTDF
  • 184
  • 1
  • 17
  • Possible duplicate of [PHP: $\_SERVER variables: $\_SERVER\['HTTP\_HOST'\] vs $\_SERVER\['SERVER\_NAME'\]](https://stackoverflow.com/questions/13772934/php-server-variables-serverhttp-host-vs-serverserver-name) – Obsidian Age Jun 23 '17 at 02:53
  • @Obsidian Age How that thread can be "possible duplicate" with issue above? – voodoo417 Jun 23 '17 at 02:57
  • @Obsidian Thanks for the suggestion, but this question related to why these two variables are different, my question is why my URL is different than these variables (at least HTTP_HOST should be the same for what I understand) – TheTrueTDF Jun 23 '17 at 02:57

1 Answers1

0

Well that top interesting rewrite block you mentioned is removing the www. from the URL with a 301 redirect. So there's no way of a request getting to your PHP script with the www. intact, because any request including the www. is issued a 301 redirect and never gets to the PHP script. The browser is then re-issuing the request based on the 301 redirect, and now it gets to your script without the 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.

  • Thanks for your answer, the thing is we actually want this redirection to work, but right now when we call the page www.domain.com it stays in www.domain.com. As I didn't find any way to make this redirection work, I thought I could write a redirection in PHP instead but I am not able to detect if the requested URL starts with www or not (since $_SERVER['HTTP_HOST'] value is "domain.com"). I hope this is more clear. EDIT: the htaccess redirection do work for http but not https – TheTrueTDF Jun 23 '17 at 04:09