1

I am creating multiple website in my local webserver. I have trouble when loading images form a css files dynamically when using subfolders.

This is my structure:

Root folder
    site1
        index.php (loads templates using Smarty)
        .htaccess
        public_files
            css
            js
            img
            templates

This is how my .htaccess looks like:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-zA-Z]+)$             index.php?uri=$1 [L]
RewriteRule ^(css|js|img)/(.*)$       public_files/$1/$2 [L]

Loading my css does work:

<link href="css/style.css" rel="stylesheet" />

But within this css I cannot load images

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

I think this is because this line is called from the css folder. But when I use /img/... it will look in the root, which is also not the right place because I'm within a subfolder.

2 Answers 2

1

Keep your site1/.htaccess like this:

RewriteEngine On
RewriteBase /site1/

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-zA-Z]+)$ index.php?uri=$1 [L]

RewriteCond %{SCRIPT_FILENAME} !-f    
RewriteCond %{REQUEST_URI} !/public_files/ [NC]
RewriteRule (?:^|/)css/(img/.+)$ public_files/$1 [L,NC]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/public_files/ [NC]
RewriteRule (?:^|/)((?:css|js|img)/.+)$ public_files/$1 [L,NC]
Sign up to request clarification or add additional context in comments.

7 Comments

This way even my css isn't loaded.
Can you open your page in Firebug and check the Net tab and then tell me what URL you get for css that generates 404 for you?
Now it also tries to open http://localhost/site1/css/img/background.jpg and it got a Internal Server Error
It is looking at http://localhost/site1/css/img/background.jpg so from the css folder.
Also I got the message: Not found: /site1/public_files/css/img/background.jpg
|
0

url("img/background.jpg") says that img/ is relative to the directory holding the css (css/, I presume). So it could be looking in /css/img/. Try ../img/ instead.

1 Comment

Yes, that is the reason i use the .htaccess file. I don't want to change every path when I move my website to the root folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.