I think I understand what you are referring to now (although an example "absolute path" of how you are referencing your assets would be useful). I assume you are referencing your assets something like /img/image.jpg
(or https://example.com/img/image.jpg
)? Whereas the actual resource is located at /monica/public/img/image.jpg
? So, you basically need to rewrite all your URLs (for static assets) to the /monica/public
subdirectory. But doesn't this also affect your inter-page URLs? (Unless you are using solely relative URLs between pages?)
For example, instead of your existing <Directory>
block, try the following. Note the Directory
path references your document root.
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+\.(?:jpg|png|css|js))$ /monica/public/$1 [L]
</Directory>
The purpose of the check for file extensions (of your static assets) is to avoid the "file exists" check on every request (which is relatively expensive).
This assumes that you have a .htaccess
file located at /monica/public/.htaccess
(as shown in your file structure) that then routes the request. (Otherwise this could result in a rewrite loop if the asset really doesn't exist.)
If I make the DocumentRoot /var/www/html/monica/public/
it works correctly but I want to access it from a subdirectory, something like localhost/monica/
However, this sounds like you should "fix" your URL-paths (routing within Laravel) instead of rewriting your resources?
UPDATE:
can you tell me how would I do that?
I'm not sure, this would seem to be a Laravel/PHP issue? The document root would be /var/www/html/monica/public/
(filesystem path), but you want to effectively serve your content from a /monica
subdirectory in the URL-path. Note that "monica" in /var/www/html/monica/public/
is a different "monica" to the one in your URL. You could have your webapp in a /monica
subdirectory on the filesystem (so the absolute filesystem path would be /var/www/html/monica/public/monica
) OR you change all the URLs in your webapp to include /monica/
at the start of the URL-path.
Regardless of how you do it, all the URLs in your webapp (for static resources and everything) need to start /monica
. So, I don't see any way other than modifying your routing within Laravel.
Apache can help with moving content from say the document root to the /monica
subdirectory by issuing an external redirect. But this is only for SEO benefit, not to be used all the time by your users (slower, extra load on server, etc.).
If I change it to this ... the assets stop loading again. Do you have any idea why?
<Directory /var/www/html/monica>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+\.(?:jpg|png|css|js))$ /public/$1 [L]
</Directory>
Because (if my assumption at the start of my answer is still correct) you are referencing your assets like /img/image.jpg
and not /monica/img/image.jpg
.