I've been focusing on my very own mvc framework to help my web application learning, but am getting trouble serving static assets. I'm trying to possess a single access point in to the application, also known as a front controller, so during my project / I've an .htaccess file that redirects all demands towards the application/ folder where another .htaccess passes the request uri to index.php (in application/) who associates the request towards the appropriate remotes.

However, after i attempt to offer static content, for example javascripts or cascading down style sheets, I get rerouted through application/index.php. I'm also getting "favicon.ico doesn't appear in /var/world wide web" errors in /var/log/apache2/errors.log (maybe due to symlink to ~/world wide web?). I don't be prepared to due to the next .htaccess file within the root directory of my project root:

<IfModule mod_rewrite.c>
  Options -Indexes +FollowSymLinks
  RewriteEngine On

  RewriteBase /

  # ----------------------------------------------------------------------
  # Suppress the "www." at the beginning of URLs
  # ----------------------------------------------------------------------
  # The same content should never be available under two different URLs - especially not with and
  # without "www." at the beginning, since this can cause SEO problems (duplicate content).
  # That's why you should choose one of the alternatives and redirect the other one.
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

  #----------------------------------------------------------------------
  # Route static resources to respective files
  #----------------------------------------------------------------------
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond public/$0 -f
  RewriteRule ^.+\.(jpg|gif|png|ico|css|js)$ /public/$0 [L]

  #----------------------------------------------------------------------
  # Redirect all other requests to the app folder
  #----------------------------------------------------------------------
  RewriteRule   ^$    app/   [L]
  RewriteRule   (.*)  app/$1 [L]
</IfModule>

here is the .htaccess during my application/ folder:

<IfModule mod_rewrite.c>
  RewriteEngine On

  # ensure request is not path to filename or directory
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d

  # redirect all requests to index.php?url=PATHNAME
  RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Why can't I serve static content properly? This could seem sensible in my experience basically wasn't attempting to sent all static demands to public/, that is where my css, digital, png, js, etc files reside. But I've got a RewriteCond rule inside to transmit the demands for such files towards the public dir... Confusing?

Presuming, from things i understood, that the project structure may be the following:


/

/.htaccess

/application/.htaccess

/application/index.php

/public/static.js (for instance)

Here's what I develop, wishing it'll solve your condition: the .htaccess within the root folder:

<IfModule mod_rewrite.c>
    Options -Indexes +FollowSymLinks
    RewriteEngine On

    RewriteBase /

    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    RewriteRule ^public/.+\.(jpg|gif|png|ico|css|js)$ - [L]
    RewriteRule ^(.*)$ app/$1 [L]
</IfModule>

And also the .htaccess within the application folder is unchanged.

Every request beginning with public and as being a file using the listed extensions will not be rerouted that is completed with the dash character. The final rule enables to redirect a request towards the application/index.php file.

I believe the resulting behavior may be the expected one:

  • static files within the public directory aren't rerouted,
  • files with another extension within the public directory is going to be rerouted to application/index.php (maybe for many error treatment),
  • demands not beginning with public is going to be rerouted to application/index.php.