1

I am struggling to get my website's CSS, IMG and JS resources to show. My website has this structure:

root
   classes
   templates
   public
    images
    scripts
    styles
    index.php
   Procfile
   apache.conf

Now, I know this problem is caused by my apache rewrites because when I include them, my PHP works but my images don't. When I don't include them, my PHP is broken by the assets work. Strangely thought I had to add a slash to the front of my rewrite rule, as the originally recommended index.php did not work and had to be changed to /index.php

My Proc file sets up public to be the root, and my rewrites look like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [NC,L]

With the above, my PHP code works fine. My question is, how please can I get it so the rewrite results don't break all my assets from loading? When I view the source of the site, they are located in the right place, just I think they are being blocked from loading.

Jimmy
  • 269
  • 4
  • 7
  • 23

1 Answers1

1

The apache.conf has the apache rewrites written above

apache.conf would seem to be your "server config". When mod_rewrite directives are used in a server context, as opposed to a directory (.htaccess) context they work differently and can require a different syntax. This explains why you needed to prefix the substitution string with a slash in order to make it relative to the DocumentRoot (without the slash it is seen as relative to the server root).

Consequently, the REQUEST_FILENAME server variable cannot be used as-is in a server context, since the request has not yet been mapped to the filesystem. REQUEST_FILENAME will be the same as REQUEST_URI and your filesystem checks will fail (resulting in your static resources being rewritten).

To get the final value of the REQUEST_FILENAME variable in a server context, you need to use a URL-based look-ahead, ie. %{LA-U:REQUEST_FILENAME}.

Try the following instead:

RewriteEngine On
RewriteCond %{LA-U:REQUEST_FILENAME} !-s
RewriteCond %{LA-U:REQUEST_FILENAME} !-l
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
MrWhite
  • 12,647
  • 4
  • 29
  • 41