codeigniter .htaccess code not loading css, images

2019-08-30 15:07发布

问题:

I am using the htaccess provided by codeigniter in their website. Everything works file, but when i use a / after the url, everything gets messed up. say my domain is www.example.com/mycontroller this will work fine but www.example.com/mycontroller/ makes every thing messed up, no style no image and all that.

I solved the issue by using base_url() in front of the links for stylesheet and images etc. But isnt there any other way so that I dont have to go through every image and link to add base_url().

The .htaccess file contains

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

回答1:

You should allow files and folders to pass

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond $1 !^(index\.php|css|images|robots\.txt)
RewriteRule ^(.*)$ ./index.php/$1 [L]

And yes, you should add base_url, otherwise your application is less portable and maintenance becomes a nightmare



回答2:

Try to check if the request points to a file or a directory:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|images|robots\.txt)
RewriteRule ^(.*)$ ./index.php/$1 [L]

If you don't have some realy good reasons you can link relative to the base directory for images or css files.