codeigniter 404 not found CSS

2019-08-30 11:06发布

问题:

I have an issue about CSS url in codeigniter:

In file controllers I have the file admin/home.php

in home.php :

class Home have function index() and function login().
function login() { redirect('admin/home');}

file config:

$routes['admin'] = 'admin/home/login';
$routes['admin/home'] = 'admin/home/index';

and error:

Request URL:http://congnghehatnhan.tk/admin/public/css/bootstrap.min.css

Request Method:GET

Status Code:404 Not Found

回答1:

You most likely have the following syntax when describing your site assets:

<link href="public/css/bootstrap.min.css" rel="stylesheet">

Your problem occurs when you are on the following URL:

http://congnghehatnhan.tk/admin/

A side-effect of using the stylesheet declaration directly as you have, results in the fact that the stylesheet is currently linking to:

http://congnghehatnhan.tk/admin/public/css/bootstrap.min.css

You however want it to link to:

http://congnghehatnhan.tk/public/css/bootstrap.min.css

Notice the missing admin segment from the URL.

When referencing assets via CodeIgniter, you want to use the base_url() method in order to generate the correct link. Thus, without knowing more about your codebase, I believe that changing the original stylesheet declaration to the following should fix your issue:

<link href="<?php echo base_url('public/css/bootstrap.min.css');?>" rel="stylesheet">

Please note that the approach above applies to all instances of referencing assets.



标签: codeigniter