Skip to content
Advertisement

htacess redirecting to js & css files

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

Advertisement

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/

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement