Skip to content

less strict string matching in magento-vars.php #446

Open
@joeshelton-wagento

Description

@joeshelton-wagento

The isHttpHost() function has very strict string matching. This requires a new block of code utilizing the function to be created for each new environment.

For example:

if (isHttpHost("www.examplea.com")) {
    $_SERVER["MAGE_RUN_CODE"] = "default";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}
if (isHttpHost("mcstaging.examplea.com")) {
    $_SERVER["MAGE_RUN_CODE"] = "default";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}
if (isHttpHost("examplea.branchname-zrgukpa-kolnt6awfmkyd.us-3.magentosite.cloud")) {
    $_SERVER["MAGE_RUN_CODE"] = "default";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}
if (isHttpHost("examplea.test")) {
    $_SERVER["MAGE_RUN_CODE"] = "default";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}
if (isHttpHost("www.exampleb.com")) {
    $_SERVER["MAGE_RUN_CODE"] = "storeb";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}
if (isHttpHost("mcstaging.exampleb.com")) {
    $_SERVER["MAGE_RUN_CODE"] = "storeb";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}
if (isHttpHost("exampleb.branchname-zrgukpa-kolnt6awfmkyd.us-3.magentosite.cloud")) {
    $_SERVER["MAGE_RUN_CODE"] = "storeb";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}
if (isHttpHost("exampleb.test")) {
    $_SERVER["MAGE_RUN_CODE"] = "storeb";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}

Also, the default function does not accommodate domain name differentiators that may occur at the end of the domain name. For example:

  • example.com
  • example.co.uk

I propose a more flexible string matching that would allow for one block of code to identify a store in all environments. For example:

if (isHttpHost("examplea")) {
    $_SERVER["MAGE_RUN_CODE"] = "default";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}
if (isHttpHost("exampleb")) {
    $_SERVER["MAGE_RUN_CODE"] = "storeb";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}

This is a better fit for Magento's dynamic system of hostname creation.

Personally, I always change the function to have more flexible string matching in my own projects. But, I notice that many people on Community Engineering Slack mistakenly assume that the function can't be changed. They rely on the function provided and are frustrated by it.

This is my own preference:

function isHttpHost($host)
{
    if (!isset($_SERVER['HTTP_HOST'])) {
        return false;
    }
    return strpos(str_replace('---', '.', $_SERVER['HTTP_HOST']), $host) !== false;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions