skip to Main Content

This is my typical PHP MVC Structure:

-Resources
        --/my.css
        --/my.js
-Project
        --/.htaccess
        --/index.php
        --/Other Files & Folders

This is my htaccess configuration:


Options -Indexes

#AddHandler application/x-httpd-php53 .php .php5 .php4 .php3
RewriteEngine on

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteCond %{REQUEST_URI} !.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Write now my all requests are going through index.php. In my project files, I am accessing my css & js files located in Resources folder like following:

<link rel="stylesheet" type="text/css" href="/../Resources/my.css"/>
<script src="/../Resources/my.js"></script>

This result in following error

HTTP/1.1 404 Not Found 258ms

I tried to add following Rewrite rule in htaccess configuration, but it doesn’t work:

RewriteCond %{REQUEST_URI} ^/Resources/my.css
RewriteRule ^api(.*)$ ../Resources/my.css [NC,QSA,L]

I don’t want to change my folder structure, since the same CSS & JS resources are shared between multiple projects, and it’s easy to manage them from single location. I just want to reference them somehow.

I also don’t want to include the code in my project file, like : include('../Resources/my.css') in PHP, I don’t want this.

Please provide a solution to this.
Thanks and Regards

2

Answers


  1. Chosen as BEST ANSWER

    Well, as other also have pointed out in comments and answers, this is NOT POSSIBLE. We can't refer above or outside of our Project Root from client side scripting. We can do things from server side coding, but client-side code, not possible/


  2. By us it’s kind of an unwritten role to always put assets in sub-directories of root dir (the dir containing the index.php file is root).

    Anyway, you can’t rewrite to outside the document-root. This is a security thing, and just think about it, does example.com/../my-dir make any sense?


    A workaround (like in another-post) would be to create a symbol-link to your Resources dir, lets name it mySymbolLink then you could do:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{REQUEST_URI} ^Resources/.*
        RewriteRule ^Resources/(.*)$ /mySymbolLink/$1 [QSA,L]
    </IfModule>
    

    But you still need to change the paths, for example, /../Resources/my.js should be /Resources/my.js to work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search