0

After install very fresh Magento 2, I am seeing Whoops, our bad... (404 Not Found) in all pages. Screenshot: enter image description here

I am using Linux Mint OS.

Here is my steps:

  1. Installed Apache version is apache2 -v:
Server version: Apache/2.4.41 (Ubuntu)
Server built:   2020-08-12T19:46:17
  1. Checked PHP Version: PHP 7.4.18 (cli) (built: May 3 2021 11:27:06) ( NTS )
  2. Did execute: sudo apt-get install curl wget gnupg
  3. Checked MySQL Version: mysql Ver 8.0.25-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
  4. Checked composer version: Composer version 2.0.7 2020-11-13 17:31:06
  5. Did execute the following commands:
sudo apt-get install php7.4-intl
sudo apt-get install php7.4-soap
sudo apt install php7.4-bcmath
sudo apt install php7.4-xsl

sudo apt install php7.4-ctype
sudo apt install php7.4-dom
sudo apt install php7.4-gd
sudo apt install php7.4-iconv
sudo apt install php7.4-mbstring
sudo apt install php7.4-simplexml
sudo apt install php7.4-zip
sudo apt install php7.4-libxml

sudo service apache2 restart
  1. Installed and Start elasticsearch
  2. Download Magento 2 using composer, Did execute the following commands:
cd www/
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento-practice
cd magento-practice/
sudo chmod -R 777 ./

Created Database and installed the Magento 2:

php bin/magento setup:install --base-url=http://localhost/magento-practice --db-host=localhost --db-name=magento_practice \
--db-user=dbadmin --db-password=Qwerty \
--admin-firstname=admin --admin-lastname=admin [email protected] --admin-user=admin --admin-password=786Admin \
--language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1 \
--backend-frontname=adminn

Then also esecute the following commands:

sudo php -dmemory_limit=-1 bin/magento setup:upgrade
sudo php -dmemory_limit=-1 bin/magento setup:static-content:deploy -f
sudo php -dmemory_limit=-1 bin/magento indexer:reindex
sudo php -dmemory_limit=-1 bin/magento cache:clean
sudo php -dmemory_limit=-1 bin/magento cache:flush

sudo chmod -R 777 ./

Now edited the .htaccess From:

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

Into:

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

What I am doing wrong?

Edit: Sofar I understand, the .htaccess is not creating from the .htaccess.sample while configuring/setup the magento2.

6
  • Hi! Please provide more information: store URL (using example.com), full apache .htaccess and few samples of URLs that returns 404.
    – dudzio
    Commented May 30, 2021 at 19:19
  • Looks like you misconfigured your apache it should point to .../magento-root directory, and you should not edit .htaccess file. Could you add your apache's vhost config to the question?
    – dudzio
    Commented May 30, 2021 at 19:24
  • I have created a folder magento-practice as the root of the magento2 base which made URL http://localhost/magento-practice.
    – Shah Alom
    Commented Jun 1, 2021 at 9:45
  • Have you tried installing with MySQL 5.7?
    – Mohit Rane
    Commented Jun 1, 2021 at 10:32
  • It has long been recommended to make the /pub subfolder the docroot. I believe that 2.4.2 makes this mandatory. You might also check your webserver logs and/or use browser tools to see if there are any unexpected redirects happening.
    – jiheison
    Commented Jun 2, 2021 at 3:08

6 Answers 6

0

You should enable mod rewrite

sudo a2enmod rewrite;
sudo service apache2 restart;

For the virtualhost(please change the xxxx for specific site and match with your current setting up). You should let it be separated with localhost.

sudo nano /etc/apache2/sites-available/local.xxx.com.conf;
<VirtualHost *:80>
    ServerName local.xxxx.com

    DocumentRoot "/home/xxxx/Projects/xxxx"
    <Directory "/home/xxxx/Projects/xxxx">
        AllowOverride All
        Order Allow,Deny
        Allow from all
    Require all granted
    </Directory>

    ErrorLog "/home/xxxx/Projects/logs/error.log"
    CustomLog "/home/xxxx/Projects/logs/access.log" combined
</VirtualHost>
sudo a2ensite local.xxxx.com.conf;
sudo service apache2 restart;

Grand apache user to use the folders

sudo usermod -aG www-data $USER;
sudo chgrp www-data /home/xxxx/Projects;
sudo chmod -R g+rwx /home/xxxx/Projects;

Set permission for magento root folder if necessary

sudo find . -type f -exec chmod 644 {} \;
sudo find . -type d -exec chmod 755 {} \;
sudo find ./media -type d -exec chmod 777 {} \;
sudo find ./media -exec chmod 777 {} \;
sudo find ./var -type d -exec chmod 777 {} \;
sudo chmod 777 ./app/etc;
sudo chmod 644 ./app/etc/*.xml;

Keep original .htaccess you don't have to make changes.

1
  • Thank you for your answer. mod rewrite modules was enabled. still no luck. :(
    – Shah Alom
    Commented Jun 1, 2021 at 9:48
0

Option.1

go to below configuration in admin

Stores -> Configurations -> General ->Web -> Search Engine Optimization -> Use Web Server Rewrites

and disable "Use Web Server Rewrites".


Option.2

run below command from ssh

php bin/magento config:set web/seo/use_rewrites 0 
php bin/magento cache:clean

if it works for you that mean issue with php rewrite module

hope this helps you :)

2
  • Every page is showing 404 error, how can i login into admin?
    – Shah Alom
    Commented Jun 1, 2021 at 9:49
  • updated answer with option 2. please try this Commented Jun 1, 2021 at 10:21
0

Try This

Create configuration file for host

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

Now, modify the configuration files to match with our virtual hosts and put the below code.

sudo gedit /etc/apache2/sites-available/sa.com.conf

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName sa.com.tn
    ServerAlias www.sa.com.tn
    DocumentRoot /var/www/html/SA
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/html/SA>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
    </Directory>
</VirtualHost>

Enable virtual host configuration files

sudo a2ensite sa.com.conf

Restart apache web server.

sudo service apache2 restart

Add your virtual host like below and put code.

gedit /etc/hosts

After adding the below line to this file and save the file.

127.0.0.1 sa.com.tn

Now edit core_config_data table and put value web/unsecure/base_url & web/secure/base_url here like below

http://sa.com.tn/

Update

Now edited the .htaccess :

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

I Hope This Helps You.

4
  • Thank You, still no luck.
    – Shah Alom
    Commented Jun 1, 2021 at 9:50
  • @ShahAlom please change your .htaccess file like above. I update the answer.
    – Msquare
    Commented Jun 2, 2021 at 5:50
  • @ShahAlom Have you try? Please check and update me.
    – Msquare
    Commented Jun 2, 2021 at 9:20
  • I tried and still seeing the page not found error.
    – Shah Alom
    Commented Jun 4, 2021 at 22:02
0

As you have apache2 server. Can you please check your .htaccess file exist? As this file is messed up in your setup.Can you replace .htaccess file from default one ?

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

sometimes it's because of file permission and make sure rewrite is enabled

a2enmod rewrite
0

This issue has been mentioned here https://github.com/magento/magento2/issues/32285. My quick workaround is, update .htaccess in the magento base folder file following:

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

(Aim to remove the slash before pub)

1
  • Thank you for the reply. Actually I tried this and no luck :(
    – Shah Alom
    Commented Jun 4, 2021 at 21:59
0

If you're on 2.4.2 it's the magento-in-subfolder issue with the new .htaccess settings

The [magento_root]/index.php file has been removed, and Magento now runs from /pub by default for Apache configurations. Stores that are served from subfolders will not work as expected and may display 404 errors

The installation instructions bin/magento setup:install still give an example with a subfolder I flagged it on github months ago :(

simplest, if you are just starting:

  • reinstall but not in a subfolder
  • make the pub/ folder the server root
  • things also work if you make the magento install main folder the document root, there's a config option

If you can change the document root (essentially losing your subfolder)

  • if you control your server config, don't need multiple apps in one instance, and don't want a "URL key" for the shop, just change the apache configuration to make the 'document root' be the pub folder in your magento install

  • things also work if you make the magento install main folder the document root, old style way, there's a config option

  • if you still want to have a url path showing for the shop, the workaround is simlinks inside the pub folder as per https://devdocs.magento.com/guides/v2.4/release-notes/open-source-2-4-2.html#known-issues

If you can't make pub or magento the document root:

simplest remains reinstall Magento (or move it) to your doc root.

Otherwise You will need to tweak your root .htaccess rewrites to pass things to the pub/ folder not the main folder

I banged my head on walls with that one but found that on my system it works IF I MADE THE FOLDER name different from the URL structure path. i.e. if you want "myshop" in the URL then create a folder called, say, "myshop-folder". (or have no URL but a subfolder)

These are answers I gave in post https://magento.stackexchange.com/posts/332405 in March but should still work

subfolder install that is invisible in url

For testing purposes I have a test site upgraded to 2.4.2 and managed to find a config that uses a subfolder BUT the subfolder is invisible in the url.

  • magento installed in magento2-path subfolder
  • site working on www.mywebsite.com
  • if magento2-path was visible in url, and therefore set up in the site url-path config or in stylesheets, that must be cleaned out
  • www.mywebsite.com/magento2-path will 404

Note 2: to save work this does not have a local copy of the images and uses "static domain" for images so not certain it 100% correct for static files and probably some extra rule needed for pub/static

site-root
.htaccess
     magento2-path
     .htaccess
          pub
          .htaccess

the htaccess at the top has

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteCond %{REQUEST_URI} !^/magento2-path/setup/
RewriteCond %{REQUEST_URI} !^/magento2-path/update/
RewriteCond %{REQUEST_URI} !^/magento2-path/dev/
RewriteCond %{REQUEST_URI} !^/magento2-path/pub/
RewriteCond %{REQUEST_URI} !^/magento2-path/
RewriteRule .* /magento2-path/pub/$0 [L]

RewriteCond %{REQUEST_URI} ^/pub/
RewriteRule .* /magento2-path/$0 [L]

DirectoryIndex index.php

the htaccess in magento2-path is modified to (the / in front of pub is removed)

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

the htaccess in pub is not modified, except to add any special old "migration" rules I had from migration from past modules or pages that are now gone

attempt #2 subfolder (with redirect)

UPDATE: I have found that for a site that used the magento install path in the URL I can make it redirect so the page shows (without that path) only when the folder is renamed to not match the path anymore. Otherwise requests get passed down the stack. You want to make sure your "root level" rewrites are used, not the magento one.

  • magento was installed in magento2-path subfolder RENAME THIS FOLDER eg tomagento2-folder

  • site working on www.mywebsite.com

  • if magento2-path was visible in url, and therefore set up in the site url-path config or in stylesheets, that must be cleaned out

  • www.mywebsite.com/magento2-path/whatever redirects to www.mywebsite.com/whatever which displays correctly

    site-root .htaccess magento2-folder2 .htaccess pub .htaccess

the htaccess at the top has (note: this is total overkill i'm sure only half the redirects are needed but not got time to clean)

RewriteEngine on

RewriteRule ^/magento2-path/$ / [R=301,L]
RewriteRule ^/magento2-path$ / [R=301,L]
RewriteRule ^magento2-path/$ / [R=301,L]
RewriteRule ^magento2-path$ / [R=301,L]

RewriteCond %{REQUEST_URI} /magento2-path/
RewriteCond %{REQUEST_URI} !^/magento2-path/setup/
RewriteCond %{REQUEST_URI} !^/magento2-path/update/
RewriteCond %{REQUEST_URI} !^/magento2-path/dev/
RewriteCond %{REQUEST_URI} !^/magento2-path/pub/
RewriteRule ^magento2-path(.*) $1  [R=301,L]

RewriteCond %{REQUEST_URI} /magento2-path/
RewriteCond %{REQUEST_URI} !^/magento2-path/setup/
RewriteCond %{REQUEST_URI} !^/magento2-path/update/
RewriteCond %{REQUEST_URI} !^/magento2-path/dev/
RewriteCond %{REQUEST_URI} !^/magento2-path/pub/
RewriteRule ^/magento2-path/(.*) /$1  [R=301,L]


RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteCond %{REQUEST_URI} !^/magento2-path/setup/
RewriteCond %{REQUEST_URI} !^/magento2-path/update/
RewriteCond %{REQUEST_URI} !^/magento2-path/dev/
RewriteCond %{REQUEST_URI} !^/magento2-path/
RewriteRule .* /magento2-folder/pub/$0 [L]

RewriteCond %{REQUEST_URI} ^/pub/
RewriteRule .* /magento2-path/$0 [L]

DirectoryIndex index.php

the htaccess in magento2-folder and magento2-folder/pub is the same as above

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.