magento2: Magento 2.4.2 old docroot logic does not work without workaround
Hello! On the other day i had a task to install magento 2.4.2 for testing purposes. I am holding my testing shops in subdirectories. Version 2.4.2 does not work with that logic from the start because it’s following new pub docroot path. Magento is working perfectly fine if I will create a virtual host that points on pub directory. But on some testing servers i would preffer to hold different magento installations in subdirectories rather than creating a subdomains. So I tried to find the way for it to work with old logic and in my opinion I found some bugs that broke the old way of accesing magento which results into the need of implementing a workaround to work properly.
Preconditions
- Magento 2.4.2 Installation
- PHP 7.4
- Apache 2.4.18
- Elasticsearch 7.9.1
Steps to reproduce
- Install magento 2.4.2
- Make base url and apache virtual host point to magento installation in subdirectory (example 127.0.0.1/magento242/)
- Try to access 127.0.0.1/magento242. This immediately result into a 404 not found page. I looked at new .htaccess and found this line:
RewriteRule .* /pub/$0 [L]
Okay, that 404 error is expected because the request is redirected to 127.0.0.1/pub and I don’t have it on my environment. I created that folder and index file for simple test and it worked as i expected. 4. Then I think that I just need to change that line to redirect into magento pub directory.
RewriteRule .* /magento242/pub/$0 [L]
That still gave me a 404 not found error but now it was a magento error page not the apache one because files from pub directory such as error page and static files were correctly downloaded on request. I tried going to admin url and it still does not work. Okay I don’t know why that do not work so I tried to change new array key in env.php config file directories=>document_root_is_pub to false. I cleared the cache but nothing changed. 5. In the next step I decided to implement the old way of accesing magento installation. I copied .htacces_sample into magento root installation as .htaccess, copied index.php from pub into the root folder of installation and changed require path to app/bootstrap.php in it. It is important to clear the cache on this step. 6. The home and admin page works perfectly now. They gave 200 response and not a 404 error page anymore. But there are issues with static files. They now give a 404 errors because of path changes in magento 2.4.2 that i think are relative to this commit https://github.com/magento/magento2/commit/640cad53009b291334234ccd61ab79f256b43da2. I tried to change value of directories=>document_root_is_pub because I think that this config value should regulate the correct paths but nothing changed. Either it is false or true the result is the same. I looked at the code and don’t see any places where this config value would somehow be used. (I found only one place where it is set into $configData) 7. So I found 2 workarounds to this problem.
- Make a redirect on top of .htaccess to pub if certain words are met in the REQUEST_URI.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond "%{REQUEST_URI}" "(/errors/|/static/|/media/|/opt/)"
RewriteRule .* /magento242/pub/$0 [L]
</IfModule>
- Make a symlinks from magento root to folders inside the pub directory such as errors, opt, static, media
Expected result
- I expected it to work and display all the pages but instead it displays 404 errors on any link such as homepage, admin_url but static files are downloaded fine.
- Expected it to work in this step too, because I haven’t found a mention in 2.4.2 release notes that paths are changed to not include /pub/ anymore and now aren’t compatible with old way of accessing magento.
Actual result
I didn’t find how to add a label for Severity so I decide to leave it here. I think that is Severity: S1
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 19 (13 by maintainers)
We’ve performed an investigation and found the solution with symlinks (https://magento.stackexchange.com/a/332419/38801) to be working fine on 2.4.2. We will proceed with official documentation for it.
Please let me know if there any other use cases that need investigation.
@shiftedreality I got rid of the subfolders but I can’t help but notice that the installation instructions https://devdocs.magento.com/guides/v2.4/install-gde/composer.html are STILL GIVING EXAMPLES THAT LEAD TO GENERAL 404
These need to be modified to something that works on 2.4.2
Because people are following these instructions and you can find them on stack exchange “fresh install doesnt work” https://magento.stackexchange.com/questions/337808/magento-2-fresh-install-showing-whoops-our-bad-404-page-not-found/339249
@hostep thanks for noticing this, changed to link on DevDocs instead of private PR
You still can use
index.php
from the root folder.We’ve adjusted root
.htaccess
to point topub
folder in a backward-compatible way. Here is more on the change: https://github.com/magento/magento2/pull/29176So, in version 2.4.2+ you still have 3 options as before:
.htaccess
configured to point topub
: https://github.com/magento/magento2/blob/2.4.2/.htaccesspub
rootpub
rootSince having Magento in subdirectories is not officially supported and tested, I would encourage you to use a workaround and create a pull-request into DevDocs with the preferred method.
Otherwise, we do not have any tests or tools in place to verify this installation method.
This .htaccess rule works for me (remove the slash before pub/):