2

I have an problem about magento 2.4.2 Installed magento 2.4.2 using Composer-CLI. But I can't access magento homepage (http://localhost:8888/magento2)

The requested URL /pub/ was not found on this server.

enter image description here

My .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

Changed .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

If I change the RewriteRule from .* /pub/$0 [L] to .* pub/$0 [L] , i can see magento home page with some errors.

The page you requested was not found, and we have a fine guess why. If you typed the URL directly, please make sure the spelling is correct. If you clicked on a link to get here, the link is outdated.

enter image description here

How can solve this problem?

Said
  • 31
  • 1
  • 2
  • Change your webroot to the "pub" directory in the apache config file. – Amit Saini Mar 04 '21 at 09:23
  • @AmitSaini How to do ? Please show here or post your answer. – Msquare Mar 04 '21 at 09:36
  • @Said Setup apache document root like this https://devdocs.magento.com/guides/v2.4/install-gde/tutorials/change-docroot-to-pub.html#1-edit-your-server-configuration – Amit Saini Mar 04 '21 at 09:44
  • Using RewriteRule .* pub/$0 [L] instead of RewriteRule .* /pub/$0 [L] worked for me... Home page is getting loaded properly but all inner pages are throwing error like 404 – zuber bandi Aug 15 '21 at 07:11

2 Answers2

2

I think your problem is already known issue when using 2.4.2 for the first time and with a setup that doesn't point to /pub folder (the webserver doesn't know that it has to serve the requests from the /pub folder)

I suggest you create a virtual host dedicated solely to your magento 2.4.2 instance:

  1. enter/ create the Apache config by running vim /etc/apache2/sites-available/000-default.conf
  2. update the 'DocumentRoot' directive to point to 'pub' folder (note that you need to replace '/var/www/html/magento2ce' with the path to your magento installation):
<VirtualHost *:80>
         ServerName magento.local
         ServerAdmin webmaster@localhost
         DocumentRoot /var/www/html/magento2ce/pub
     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined

     &lt;Directory &quot;/var/www/html&quot;&gt;
           AllowOverride all
     &lt;/Directory&gt;

</VirtualHost>

3.a. if you didn't have the virtual host, then run sudo a2ensite 000-default.conf, otherwise, skip this step.

3.b. restart Apache by running systemctl restart apache2 command

3.c. if you didnt have the virtual host before, then add the new website to your hosts vim /etc/hosts and add the line 127.0.0.1 magento.local inside it

  1. update the magento base URL - I see you appended a directory name to your server’s hostname when you installed Magento (http://localhost:8888/magento2), you’ll need to remove it and change it to the virtual host name. This can be achieved by logging into the database and running the command:
UPDATE core_config_data SET value='http://magento.local/' WHERE path='web/unsecure/base_url';
UPDATE core_config_data SET value='http://magento.local/' WHERE path='web/secure/base_url';
  1. make sure you are serving the requests from the 'pub' folder by checking the 'env.php' file, it should contain the following piece:
'directories' => [
    'document_root_is_pub' => true
]

Source: the documentation https://devdocs.magento.com/guides/v2.4/install-gde/tutorials/change-docroot-to-pub.html.

Diana
  • 5,137
  • 1
  • 11
  • 26
  • Please give solution not link – Msquare Mar 04 '21 at 10:14
  • I updated my answer, but surely if someones checks the link will find the same steps. – Diana Mar 04 '21 at 10:29
  • I install in to my local pc and there are so many magento installed, so how to manage multiple magento setup with magento 2.4.2 – Msquare Mar 04 '21 at 10:33
  • for the m2.4.2 install you will have to have a dedicated virtual host – Diana Mar 04 '21 at 10:34
  • i am not understanding what you say. please explain bit more. – Msquare Mar 04 '21 at 10:43
  • just follow my instructions above. you need to have a dedicated virtual host for your m2.4.2 installation and not run it like 'http://localhost/magento2'. if you don't create a separate virtual host, then all other installations will cease to work. – Diana Mar 04 '21 at 10:48
  • @DianaBotean thank u so much it worked great – jassi Mar 05 '21 at 11:03
0

You can create a Virtual Hosts following this instruction: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-18-04-quickstart