CI 3 URL 路由

CI 3 URL Routing

我想通过单击按钮查看另一个页面。在页面的第一次加载期间,我可以成功查看页面所有 css 并且 js 没问题。但是,当我单击 link 时,它会重定向到另一个页面,但我无法加载 css 和 js 文件。

<a href="users/login" class="pull-right login">Login / Register</a> 当我单击此 link 时,它会将我重定向到 http://localhost/sampleproject/users/login 但 css 和 js 无法加载。在我的登录视图中,我有一个后退按钮,可将我重定向到索引

<a href="home">Back</a>

但是当我点击 link 时它会转到 http://localhost/sampleproject/users/home which should be http://localhost/sampleproject/home

在我的 .htaccess 文件中

RewriteEngine On

RewriteCond  !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/ [L,QSA]

请告诉我哪里出了问题。我是 ci 的新手。我想尝试从头开始做一个新项目。谢谢!

使用Codeigniter的方法来实现。 site_url() 可用于 links

<a href="<?php echo site_url('home');?>">Back</a>

对于资产(CSS、JS、图像 Link 等)link 使用 base_url()

<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/style.css');?>">

假设您在项目根目录中有一个名为 assets 的文件夹。

还要确保设置

$config['base_url'] = 'http://localhost/sampleproject/';

application/config/config.php