在CodeIgniter中没有/index.php/链接到CSS

Sai*_*men 1 php codeigniter

我有没有/index.php/ URL的CodeIgniter,我想在视图中链接CSS文件.

我的.htaccess:

RewriteEngine on
RewriteCond $1 !^([a-zA-z0-9/])
RewriteRule ^(.*)$ index.php [L]
RewriteCond $1 !^(index.php|images|robots.txt|system|user_guide)
RewriteRule ^(.*)$ index.php/$1 [L]
Run Code Online (Sandbox Code Playgroud)

我的链接:

<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>application/views/metrohacker/style.css" />
Run Code Online (Sandbox Code Playgroud)

使用/index.php/它可以工作,但我想要简单的URL ...

Chr*_*itz 7

这是因为您的应用程序文件夹不在htaccess文件中未重写的文件夹列表中.

RewriteCond $1 !^(index.php|images|robots.txt|system|user_guide)
Run Code Online (Sandbox Code Playgroud)

我会将CSS文件保存在应用程序的根目录中而不是视图文件夹中.我也会对你的JavaScripts和图像做同样的事情.那样你的文件夹结构就像这样:

root/
    application/
    assets/
        css/
        js/
        images/
    system/
...
Run Code Online (Sandbox Code Playgroud)

确保将assets文件夹添加到.htaccess:

RewriteCond $1 !^(index.php|images|robots.txt|system|user_guide|assets)
Run Code Online (Sandbox Code Playgroud)

然后你不必担心index.php做路由,你可以像这样加载一个CSS文件:

<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/css/style.css" />
Run Code Online (Sandbox Code Playgroud)