Magento, Magento 2

Solved : 404 not Found After Fresh Magento 2.4.2 Installation

Solved : 404 not Found After Fresh Magento 2.4.2 Installation

In this tutorial, Today I will explain to how to solve issue of 404 not found after fresh Magento 2.4.2 installation. From Magento 2.4.2, Magento modified docroot to improve security and because of that, now if you execute base url then, you will see that it’s return 404 not found error.

To solve this issue, you need to create virtual host to run Magento 2.4.2. Just follow the below steps to create virtual host :

You may also like this :

1. Copy .conf file for virtual domain :

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

2. After that, edit your example.com.conf file using this below commad :

sudo nano /etc/apache2/sites-available/example.com.conf

and then, paste the below code :

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName example.com
    ServerAlias example.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/m242ee/pub/
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

/var/www/html/m242ee/pub/ = Your Magento instance pub folder path

3. After that, you need to enable site using below command :

sudo a2ensite example.com.conf

4. Then, setup your host file using this command :

and add 127.0.0.1 example.com line like

127.0.0.1   localhost
127.0.1.1   test-desktop
127.0.0.1   example.com

5. update url in core_config_data table :

UPDATE `core_config_data` SET `value` = 'http://example.com/' WHERE `core_config_data`.`path` = 'web/unsecure/base_url';
UPDATE `core_config_data` SET `value` = 'http://example.com/' WHERE `core_config_data`.`path` = 'web/secure/base_url';
UPDATE `core_config_data` SET `value` = 'http://example.com/' WHERE `core_config_data`.`path` = 'web/unsecure/base_link_url';
UPDATE `core_config_data` SET `value` = 'http://example.com/' WHERE `core_config_data`.`path` = 'web/secure/base_link_url';

6. In last, restart your apache server :

sudo service apache2 restart

Here you can see that example.com is virtual site name. You can use any of name instead of example.com in all 6 steps.

Why this type of issue faced?

It’s because, everything is running from pub folder in new version. You can check m2.4.2-ee or m2.4.2-ce .htaccess file

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule .* /pub/$0 [L]
DirectoryIndex index.php

There are major difference in new .htaccess and old version .htaccess file. If you replace old version’s .htaccess file then, still you can’t running proper.

So, Virtual host is only one solution for now. Using this solution, you can run your new m2.4.2 version and also old version of Magento if you have installed already.

That’s it !!!

I hope this blog is easy to understand about how to solve issue of 404 not found after fresh Magento 2.4.2 installation. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Stay Safe and Stay Connected !!

Tagged ,