0

So - I have made a website with php includes in my header I have the following

<?php
$protocol = (!empty($_SERVER['HTTPS'])) ? 'https' : 'http';
$path = $protocol . '://domain.com/';
?>

but for some reason when I put www. in the URL bar, it breaks the site I even have a 301 direct in cpanel

is there a way to add www or no www to this?

op1001
  • 348
  • 2
  • 8
  • 15
  • Rewriting to include httpS and/or www is usually easiest by using `.htaccess` and letting the web server handle rewriting things. – Dave Jun 08 '19 at 14:29
  • this is how the site was developed from whomever i have been working with it, and within the the site all images etc are being called to the $path, it would take too long to change everything – op1001 Jun 08 '19 at 14:31
  • If `$path` is used to load images and resources from your own site, why aren't you using a relative path? Can't you just set `$path` to `/`? – rickdenhaan Jun 08 '19 at 15:41
  • See my answer.... – Alessandro Jun 08 '19 at 15:48
  • www. is part of the host – Alessandro Jun 08 '19 at 15:54
  • It is a good practice to have one canonical host (e.g. I prefer non-www since it’s just shorter) and redirect other variants to the canonical one (e.g. from www to non-www). – Marat Tanalin Jun 08 '19 at 16:05

1 Answers1

0

It's possible to have a function for absolute http(s) path, the best way is check if server responses as HTTP or HTTPS and rewrite the php code according to new path...

check.php

<?php
  // Checking for HTTP or HTTPS
  function check_protocol() {
    $isSecure = false;
    if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
      $isSecure = true;
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) == 'on') {
      $isSecure = true;
    }
    return $isSecure;
  }
  $protocol = check_protocol() ? 'https://' : 'http://';
  $host = $protocol . $_SERVER['HTTP_HOST'];
?>

Usage is simple:

<?php
  include("check.php");
  echo $host;
  // it prints http://www.example.com or https://www.example.com
?>

or using the absolute path thanks to $_SERVER['DOCUMENT_ROOT']:

<?php
  include($_SERVER['DOCUMENT_ROOT'] . "/php/check.php");
  echo $host;
  // it prints http://www.example.com or https://www.example.com
?>

In addition to this may be useful a redirect using .htaccess if the server is hosted on Apache server...

I found useful this rule for my .htaccess:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

This rule may not work on different server configuration... read this thread for getting more info about the most different sever configuration...

I hope this helps.

Alessandro
  • 900
  • 12
  • 23
  • Yeh the above code doesn't work when they put www. after the http://www. breaks anything that relies on $host , i can't figure it out, even in cpanl we added the redirect with either www or not www – op1001 Jun 08 '19 at 16:04
  • Investigate on your VirtualHost – Alessandro Jun 08 '19 at 16:12
  • All servers can handle www or not without problems, see ServerName and ServerAlias into your VirtualHost... read [this](https://serverfault.com/questions/155658/apache-virtual-host-config-www-vs-non-www-rewrite-or-sserveralias) or [this](https://serverfault.com/questions/191218/apache-isnt-respecting-the-servername-directive-for-a-particular-site) or [this](https://stackoverflow.com/questions/1100343/apache-redirect-from-non-www-to-www) – Alessandro Jun 08 '19 at 16:23
  • when i add this for a example .. it loads nothing... your example does work though with i use the check.php – op1001 Jun 08 '19 at 16:25
  • Happy to hear that...Glad to help! – Alessandro Jun 08 '19 at 16:42
  • the issue - i was trying it on xampp first and it was in a sub folder so it was only loading http://localhost and not the sub folder not sure why, but on the live site it works. do you think i have to do something different for local server setup with folders? I'll probably have to change the $host to the actually URL i think – op1001 Jun 08 '19 at 16:51
  • if you replicate the structure of the server you have online I think not – Alessandro Jun 08 '19 at 16:57
  • some adaptations should be made only if you really have a different folder structure than your local server – Alessandro Jun 08 '19 at 16:58
  • http.//localhost/ is to be considered an absolute path, do not confuse it with `$_SERVER ['DOCUMENT_ROOT']` which is another thing and does not concern the loading of the contents http ... – Alessandro Jun 08 '19 at 17:02
  • If you write http://localhost/images/logo.png wherever you are you will be able to see the image as long as the url is correctly written... – Alessandro Jun 08 '19 at 17:06
  • With `Image` you should see the image correctly... – Alessandro Jun 08 '19 at 17:07
  • If you have problems including `check.php` from another folder you can use `include($_SERVER['DOCUMENT_ROOT'] . "/php/check.php");` this an absolute path for including php files... – Alessandro Jun 08 '19 at 17:16