Magento2.4 run from root

404 Not Found after Magento 2.4 installation

We will discuss how to execute the Magento2.4 run from root.
Magent2.4.x by default does not load from the root of Magento.

404 Not Found after fresh Magento 2.4 installation

It seems now Magento will only load from pub folder.

So the best solution for this is to create a virtual host and point it to pub directory.

Like earlier we were using the base_url as http://127.0.0.1/magento.

So instead of this now we need to use http://127.0.0.1/magento/pub as the base_url.

Secure and Unsecure URL needs to be setup on the basis of this.

Alternative Solution 1

1. Copy .httaccess file from pub folder and override it to root .htaccess file

2. Upload pub/index.php file to root magento directory

  
  Replace from :-

  require __DIR__ . '/../app/bootstrap.php';

  To:-

  require __DIR__ . '/app/bootstrap.php';
  

3. Add below code in app/etc/env.php file

'system' => [
    'default' => [
        'web' => [
            'unsecure' => [
                'base_media_url' => '{{secure_base_url}}pub/media/',
                'base_static_url' => '{{secure_base_url}}pub/static/'
            ],
            'secure' => [
                'base_media_url' => '{{secure_base_url}}pub/media/',
                'base_static_url' => '{{secure_base_url}}pub/static/'
            ]
        ]
    ]
],

Run setup upgrade command

Alternative Solution 2

1. Copy .httaccess file from pub folder and override it to root .htaccess file

2. Upload pub/index.php file to root magento directory

  
  Replace from :-

  require __DIR__ . '/../app/bootstrap.php';

  To:-

  require __DIR__ . '/app/bootstrap.php';
  

3. add below code in index.php file

$params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = array_replace_recursive(
    $params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] ?? [],
    [
        DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
        DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'pub/media'],
        DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'pub/static'],
        DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'pub/media/upload'],
    ]
);

4 after update index.php file look like below

use Magento\Framework\App\Bootstrap;

try {
    require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    ------------------------------
    ------------------------------
    ------------------------------
}

$bootstrap = Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */

/* code start here */

$params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = array_replace_recursive(
    $params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] ?? [],
    [
        DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
        DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'pub/media'],
        DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'pub/static'],
        DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'pub/media/upload'],
    ]
);

/* code end here */

$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
$bootstrap->run($app);

You can check the Default Configuration path in the following file:

MAGENTO_ROOT/vendor/magento/framework/App/Filesystem/DirectoryList.php

Magento2.4 run from root

Related blogs – Usefull command list in Magento 2

Like us on Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top