In pub/index.php, the following code is used to set change URLs for media and static assets to omit "/pub":

    DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
    DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'media'],
    DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'static'],
    DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'media/upload'],
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

Unfortunately in CLI scripts (bin/magento) there is nothing like this.

I found the class Magento\Setup\Mvc\Bootstrap\InitParamListener with the following code:

$initParams = $application->getServiceManager()->get(self::BOOTSTRAP_PARAM);
$directoryList = $this->createDirectoryList($initParams);

But I don't understand where this Zend Service Manager retrieves the data and how it might be possible to influence it.

My concrete problem is that an indexer has to generate full URLs and currently does it like http://magento.local/pub/media/..., where I need it to be http://magento.local/media/...

How can I tell the CLI that my virtual host points to pub and "pub" should not be part of URLs?

Ideally this should work without having to rely on additional CLI parameters or environment variables.

  • 2
    This has recently been reported as a bug: github.com/magento/magento2/issues/8868 Commented Apr 5, 2017 at 20:21
  • 1
    I found that you can set this using an argument like --magento-init-params=MAGE_DIRS[media][uri]=media. This actually works for all commands (evident by stepping through code), but commands outside setup: namespace incorrectly give an error that the option does not exist. Commented Apr 5, 2017 at 20:23
  • @ScottSB Not exactly what I was hoping for, but still an interesting find, thanks for that! Commented Apr 5, 2017 at 20:29

3 Answers 3


After much travail, I found a solution using environment variables, which I know isn't what you wanted. You can set the parameters using environment variables like this (replacing $APP_ROOT with your actual Magento root):

env \
    "MAGE_DIRS[base][path]=$APP_ROOT" \
    "MAGE_DIRS[pub][uri]=" \
    "MAGE_DIRS[media][uri]=media" \
    "MAGE_DIRS[static][uri]=static" \
    "MAGE_DIRS[upload][uri]=media/upload" \
    bin/magento indexer:reindex

Note that it's necessary to specify the base path as above because when you use environment variables, your MAGE_DIRS definition overwrites the initial default that includes that. It will, however, get merged with the fuller default config in \Magento\Framework\App\Filesystem\DirectoryList.

Credit to this answer from the chief architect himself for putting me on the scent:

How to change Magento's "var" directory to other place in Magento 2?

  • How do you use environment variables? Are these getting added into VHOST like the referenced answer?
    – harri
    Commented Jun 5, 2018 at 15:48
  • @harri I don't understand your question. Because this talking about a CLI command, a vhost isn't relevant, as the web server isn't involved. The environment variables are passed right on the command line as illustrated (arguments to env in the format FOO=bar get set as environment variables for the command at the end, in this case bin/magento). Commented Jun 5, 2018 at 19:11
  • Ah for my i'm getting this issue with a method run via cron so not sure if there is a way to wack this in with my bin/magento cron:run in crontab?
    – harri
    Commented Jun 6, 2018 at 7:17
  • Just take what I have in the answer and replace indexer:reindex with cron:run (and still update $APP_ROOT too of course). Then drop that whole thing into the crontab. Not sure if crontab will like the newlines, so you could just replace them with spaces (removing the trailing backslashes in that case). Commented Jun 6, 2018 at 23:02
  • I think better solution is here magento.stackexchange.com/a/174503/41325 Commented Jul 23, 2019 at 12:20

Another, simpler option is to simply hard-code the media path in the store config under Stores > Configuration > Web to:




Due credit to https://github.com/magento/magento2/issues/9111#issuecomment-292002922


I found another way to set MAGE_DIRS via CLI, just copy




And now you can change default directory of all internal files.

  • thx this helped me to change the cache folder for cli and webapp ;D
    – ZFNerd
    Commented Oct 22, 2018 at 12:44
  • Actually I belive this is the best solution of this problem. Official Magento guide recommends to use /lib/internal to customize the core. devdocs.magento.com/guides/v2.3/architecture/archi_perspectives/… Commented Jul 23, 2019 at 12:17
  • 1
    @A.Maksymiuk I believe you are misunderstanding that page. It is noting that the code there may be useful for extension developers, but it is not recommending customizing core directly there. Earlier on the page it says: "You should never modify Framework files, although if you are extending Magento, you must know how to call Framework libraries." Commented Jul 23, 2019 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.