I’m trying to work with CodeIgniter these days.
By default it has the index.php in URI like this
http://www.example.com/index.php/home
home is a function inside a controller
But I don’t want the index.php so I created the .htaccess file:
Allow from all
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|img|robots\.txt|script|css)
RewriteRule ^(.*)$ index.php/$1 [L]
Basically it adds “index.php” automatically except when the conds are matched.
However here’s the thing:
For URI like this: http://www.example.com/home
Everything works fine.
For URI like this: http://www.example.com/home/
Then the relative links in my webpage goes wild.
For example:
css/common.css was redirected to index.php/home/css/common.css resulting in the css file was not found.
Logs are like this:
strip per-dir prefix: [dir to my site]/home/css/common.css -> home/css/common.css
applying pattern '^(.*)$' to uri 'home/css/common.css'
rewrite 'home/css/common.css' -> 'index.php/home/css/common.css'
add per-dir prefix: index.php/home/css/common.css -> [dir to my site]/index.php/home/css/common.css
trying to replace prefix [dir to my site]/ with /
internal redirect with /index.php/home/css/common.css [INTERNAL REDIRECT]
add path info postfix: [dir to my site]/index.php -> [dir to my site]/index.php/home/css/common.css
strip per-dir prefix: [dir to my site]/index.php/home/css/common.css -> index.php/home/css/common.css
applying pattern '^(.*)$' to uri 'index.php/home/css/common.css'
pass through [dir to my site]/index.php
How can I fix this?
The problem here is not your rewrite rules – they are fine. What’s happening is that the browser thinks
/home/with a slash at the end is a real directory, so it prefixes all relative links to the directory it thinks it’s in. The browser then dashes off a request for the CSS file, with path/home/css/common.css, which, naturally, gets rewritten since it doesn’t match your check.The quickest solution I see here is to use absolute paths for CSS files. Just link them as
/css/common.cssinstead of the relative links you have now.