I had a look at a few possible duplicates for this question but rewrite rules are fairly specific to projects so I couldn’t find a suitable answer.
Crux of issue? Requests for certain files (CSS, JS & images) are being rewritten (I think) by the htaccess file.
I have an htaccess file set up to direct as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
It takes a URL like localhost/framework/booking/dashboard and rewrites to localhost/framework/index.php?controller=booking&action=dashboard
The relevant file structure inside my local www folder looks something like:
framework
-index.php
-htaccess
-views
-Booking
-dashboard.php
-wuxia-blue.css
The CSS link in dashboard.php is as follows:
<link rel='stylesheet' type='text/css' href="wuxia-blue.css">
The HTTP request being made seems to be:
/framework/booking/wuxia-blue.css
It should be:
/framework/views/booking/wuxia-blue.css
Ideally, I’d like to use something similar to
href="<?php echo(CSS . "wuxia-blue.css"); ?>".
Where CSS is a defined constant in the application and where wuxia-blue.css (and all other css files) are contained in a predefined folder of stylesheets. Obviously this would apply to other assets like .js files and images.
Any ideas?
The htaccess rule is correct – the problem is with your folder structure.
When you create a relative link, it’s relative to the current URL as displayed in the address bar – the location of the file you’re in isn’t relevant.
Your view file is at /framework/views/booking/dashboard.php, but the browser just sees the url /framework/booking/dashboard. That means the browser interprets the location of the resource to be /framework/booking/dashboard/wuxia-blue.css
Personally, I’d recommend just keeping all your css and images outside of the framework in /images and /css. Otherwise, may have to add some code in your framework to serve up image files, since all the image requests will be running through your framework.php file.