Question

I am trying to call a javascript function that is located in a ~/vendor/ directory (see schema below) without success. If I move that javascript to my directory ~/public/js/ the call succeeds.

I believe my issue might come from my .htaccess files, but I didn't find a way to fix it despite quite some readings. If anybody could point me in the right direction, I'd be very happy.

This is the directory structure (note the .htaccess on root and in public/):

<project name>/
    application/
        config/
        controllers/
        models/
        views/
    public/
        css/
        images/
        js/     <- if placed here, the call succeeds
        .htaccess
        index.php
    vendor/
        <third-party code>/
            javascript.js     <- here, it doesn't
    .htaccess

And here is the content of the .htaccess files:

  • on root directory:

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule    ^$    public/    [L]
        RewriteRule    (.*) public/$1    [L]
     </IfModule>
    
  • in public directory:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?page=$1 [PT,L]
    </IfModule>
    
Was it helpful?

Solution

I suggest having either:

public/js/Vendor
public/css/Vendor

or

public/Vendor/js
public/Vendor/css

would be the best way to go. I think it would be bad practice to rewrite the rules when you can just add the Vendor folder to public (which is where I think it ought to go).

OTHER TIPS

The only code that should sit outside the public directory is Application level code, by the looks of it your PHP.

All other assets that are loaded by your server should live in the public directory, that's why it's called public, because it's accessible by the public through your webserver's requests.

An alternative would be to create a js-source directory at the root of the project and have a task manager like Grunt or Gulp compile and copy the js into your public directory, that would look something like this:

project-name/
  application/
    config/
    controllers/
    models/
    views/
  public/
    css/
    images/
    js/     <- is filled with js from js-source/ by grunt/gulp
    .htaccess
    index.php
  js-source/
    vendor/
    main.js  <- is uglified/concatinated & copied into public/js by grunt/gulp
.htaccess

Reference links:

You could copy your htaccess from public library to your vendor library, so that it would override your root directory htaccess.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top