The duplicate question deals with .htaccess issue. I deal with the fact that /index.php may or may not be part of original URL
One way to do so is to do
http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']
The problem is if URL is http://domainname.com
the
http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']
will yield
http://domainname.com/index.php
instead.
I need something that yield http://domainname.com