Shoe
Shoe

Reputation: 76240

Url rewriting HTML and CSS paths

I have the following .htaccess file in the root of my application:

RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

This basically just convert paths such as index/var=value/var2=values2/ into index?url=index/var=value/var2=values2/ and in index.php they are elaborated to fetch $controller which is the name of the controller called (in this case index) and the $get which is the substitute for $_GET. (In this case it contains: $get['var'] = 'value' e $get['var2'] = 'value2').

Notice: In the root file, where the .htacces file and the index.php are, we have also the application/ folder (which contains controllers, views, and models), the system/ folder which initialize the framework and the public/ folder which should contains every image/javascript/css/video/audio file which can be accessed.

The .htaccess allows me to access directly every existing file both images and css. Which is great. The only problem that occurs is when the path to that image/javascript/css/audio/video file is inside the HTML or CSS files. So that when you call (*1) <img src="public/img/image.jpg"/> or when you call (*2) background-image: url(public/img/image.jpg); it just doesn't work.

That could be "solved" using absolute paths such as http://dominio.com/public/img/image.jpg but it is tricky when it comes to javascript files. And anyway is not a solution at all, but just a way to bypass the problem.

How could I make (*1) and (*2) work?

Upvotes: 2

Views: 1235

Answers (2)

encodes
encodes

Reputation: 751

background-image: url(public/img/image.jpg); 

will look in public/img path relative to the CSS file.

What you should probably be using is ../img/image.jpg or /public/img/image.jpg

Upvotes: 1

Fabrizio
Fabrizio

Reputation: 3776

Have you tried this path?

/public/img/image.jpg

EDIT/ADD

didn't test this, but you can try

RewriteCond %{REQUEST_URI} ^/public        [OR]
RewriteCond %{REQUEST_URI} ^/other_folder  [OR]
RewriteCond %{REQUEST_URI} ^/more_folders
    RewriteRule (.*) /project/$1

OR, have index.php process everything, and the very fist lines are a check to see if the called files exists based on the current path:

//untested code
$url = str_replace('..','',$_GET['url']);
if(file_exists(dirname(__FILE__).'/'.$url)){
   //redirect to that file or serve it with PHP
   exit(0);
}

Upvotes: 1

Related Questions