2

I've used routing in Codeigniter to get nice clean URLs to my pages. I also have modified the htaccess file to remove the index.php from the URL.

It's all working great except for 1 issue - When I go to a URL it won't load the CSS or Images unless the trailing / is added to the URL.

For example:

mysite.com/blah Does NOT work

mysite.com/blah/ DOES work

Probaby something in the .htaccess file? Here is my file:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|user_photos|favicon|robots\.txt)
RewriteRule ^(.*)$ /subdirectory/index.php/$1 [L]

Any ideas? Natrually I want users to be able to access the page even if they don't add the trailing /

7
  • 1
    Are you using relative or absolute paths in your includes, i.e. src="css/filename.css or src="/css/filname.css"? Commented Jan 12, 2012 at 13:00
  • My paths look like this: href="css/structure.css" or href="../css/structure.css" Commented Jan 12, 2012 at 13:10
  • Did you try changing them to absolute? Do you have the possibility to do that? Did you check Firebug's Network tab to see what files are actually request in the scenarios where the CSS's do not load? Commented Jan 12, 2012 at 13:17
  • Doesn't work when changing to absolute, the CSS doesn't load on any page. Commented Jan 12, 2012 at 13:20
  • I think it's becuase it's treating mysite.com/blah/ as it's own directory, and mysite.com/blah as in the root directory, so it's looking for the CSS in the wrong location (1 directory too high). But I don't know to stop it doing that and treat mysite.com/blah as it's own directory. Commented Jan 12, 2012 at 13:34

1 Answer 1

3

Use <?php echo base_url();?> in front of your css URL

example:

<link rel="stylesheet" href="<?php echo base_url();?>theme/css/main.css" />

As for the .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /subdirectory/index.php/$1 [L]
RewriteCond $1 !^(index\.php|includes|theme|media|robots\.txt|fonts)
Sign up to request clarification or add additional context in comments.

Comments

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.