Magento 2.4 pub folder in .htaccess

Magento 2.4 pub folder in .htaccess

August 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2.4 pub folder in .htaccess

Resolving Magento 2.4 .htaccess Issues for Pub Folder

If you’re struggling with a Magento 2.4 installation on a test server where you can’t change the root folder to ‘pub’, you’ve likely encountered an issue with the .htaccess file. Let me guide you through the steps to resolve this.

Understanding Your Current Setup

In your current configuration, you have modified the .htaccess file in the root folder like so:

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

This setup attempts to redirect all requests that don’t match certain directories (like ‘pub’, ‘setup’, etc.) to the ‘pub’ folder. However, as you’ve noticed, while the site logo and menu load correctly, the content portion returns a 404 Not Found error.

The Current Problem

The issue lies in how Magento 2 handles requests for static files like images, CSS, and JavaScript. By default, these are served directly from the ‘pub/static’ directory. Your current .htaccess configuration is not properly handling these requests when the ‘pub’ folder isn’t specified in the URL.

Solving the Issue: Redirect All Requests

To fix this, we need to configure Apache to redirect all requests that don’t match a specific pattern (like your existing RewriteCond lines) to the ‘pub’ directory. This will ensure that any request for a static file or a dynamic page is correctly routed through the Magento 2 application.

The Solution: Update Your .htaccess File

Open your root folder’s .htaccess file in an editor and replace the contents with the following:

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

Here’s what the new rules do:

  • The first line ensures that the rewrite engine is enabled.
  • The next four lines use RewriteCond to specify directories that should not be redirected. These are necessary for allowing access to the Magento 2 setup and update processes, as well as the ‘dev’ directory which contains development tools.
  • The RewriteRule line uses a regular expression ^(.*)$ to match any incoming request (except those already excluded by RewriteCond). It then redirects these requests to the ‘pub’ directory while preserving query strings using the [QSA] flag. The [L] flag tells Apache that this is the last rule; if it matches, don’t check any further rules.
  • The DirectoryIndex line sets ‘index.php’ as the default file to serve when a directory is requested without specifying a file.

Verifying Your Changes

After updating the .htaccess file, try reloading your Magento 2 site. You should now be able to access all pages and resources without having to include ‘/pub/’ in the URL.

If you’re still experiencing issues or need further assistance with your Magento setup, feel free to explore our Magento Hosting Solutions for expert support and optimized environments tailored for Magento 2.4.

Magento 2 Indexing Themes Configuration

Our network