0

So I want to load a web-app from a subfolder. Current configuration looks like this

<VirtualHost *:80>
    ServerName YOUR IP ADDRESS/DOMAIN

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/

    <Directory /var/www/html/monica/public/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted

        # Everything under this line has been added by me to try to make the redirect work
        RewriteEngine  on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$           /var/www/html/monica/public/$1  [R]
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The pages are being served correctly, but since the files contain absolute url, it keeps on failing. I'm really new to Apache so I don't know what I'm doing wrong and searching for answer has been futile.

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/

A note: This webapp was made to be be accessed from the base ( https://github.com/monicahq/monica/)

  • 1
    "The pages are being served correctly" - Can you give an example URL? Are you directly serving physical files from the filesystem? It's just that I'm looking at your `RewriteRule` directive - there's no way that can work - you can't reference an absolute filesystem path in a _directory_ context (so I guess that isn't doing anything or you've not restarted your webserver?). "but since the files contain absolute url, it keeps on failing" - "files" as in HTML files? An absolute URL is normally a good thing so I'm not sure why that is a problem - can you include an example? – MrWhite Sep 26 '18 at 13:35
  • "If I make the DocumentRoot `/var/www/html/monica/public/`" - from the information given, I can't see why that would make a difference? – MrWhite Sep 26 '18 at 13:38
  • @MrWhite Yes, I'm serving physical files.The index.php file is in the `/public/` path which references absolute path for assets like image, js, css etc and those absolute paths are based on the assumption that the root folder is `/var/www/html/monica/public/`. I can't show you a live example since the project is hosted locally, but this is the file structure https://github.com/monicahq/monica/tree/master/public – CrumpledMemories Sep 26 '18 at 14:16

1 Answers1

2

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.

MrWhite
  • 12,647
  • 4
  • 29
  • 41